android Fragments详解四:管理fragment .

本文深入探讨了如何使用FragmentManager管理Fragment,包括如何获取Fragment、执行事务操作及利用后退栈实现用户交互。重点讲解了如何通过事务添加、删除、替换Fragment,并在后退栈中保存这些变化,以便用户能够导航回事务执行前的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/nkmnkm/article/details/7172483

要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()

你可以用FragmentManager来做以上事情:


1使用方法findFragmentById()findFragmentByTag(),获取activity中已存在的fragment们。

2使用方法popBackStack()activity的后退栈中弹出fragment们(这可以模拟后退键引发的动作)。

3用方法addOnBackStackChangedListerner()注册一个侦听器以监视后退栈的变化。

更多关于以上方法的信息,请参考“FragmentManager”文档。

就像前面章节所演示的,你还可以使用FragmentManager打开一个FragmentTransaction来执行fragment的事务,比如添加或删除fragment

执行Fragment的事务

activity中使用fragment的一个伟大的好处是能跟据用户的输入对fragment进行添加、删除、替换以及执行其它动作的能力。你提交的一组fragment的变化叫做一个事务。事务通过FragmentTransaction来执行。你还可以把每个事务保存在activity的后退栈中,这样就可以让用户在fragment变化之间导航(跟在activity之间导航一样)。

你可以通过FragmentManager来取得FragmentTransaction的实例,如下:

FragmentManagerfragmentManager=getFragmentManager();
FragmentTransactionfragmentTransaction=fragmentManager.beginTransaction();

一个事务是在同一时刻执行的一组动作(很像数据库中的事务)。你可以用add(),remove(),replace()等方法构成事务,最后使用commit()方法提交事务。

在调用commint()之前,你可以用addToBackStack()把事务添加到一个后退栈中,这个后退栈属于所在的activity。有了它,就可以在用户按下返回键时,返回到fragment们执行事务之前的状态。

如下例:演示了如何用一个fragment代替另一个fragment,同时在后退栈中保存被代替的fragment的状态。

//Create new fragment and transaction
FragmentnewFragment=newExampleFragment();
FragmentTransactiontransaction=getFragmentManager().beginTransaction();

//Replace whatever is in the fragment_container view with thisfragment,
//and add the transaction to the backstack
transaction.replace(R.id.fragment_container,newFragment);
transaction.addToBackStack(null);

//Commit the transaction
transaction.commit();

解释:newFragment代替了控件IDR.id.fragment_container所指向的ViewGroup中所含的任何fragment。然后调用addToBackStack(),此时被代替的fragment就被放入后退栈中,于是当用户按下返回键时,事务发生回溯,原先的fragment又回来了。

如果你向事务添加了多个动作,比如多次调用了add(),remove()等之后又调用了addToBackStack()方法,那么所有的在commit()之前调用的方法都被作为一个事务。当用户按返回键时,所有的动作都被反向执行(事务回溯)。

事务中动作的执行顺序可随意,但要注意以下两点:

1你必须最后调用commit()

2如果你添加了多个fragment,那么它们的显示顺序跟添加顺序一至(后显示的覆盖前面的)。

如果你在执行的事务中有删除fragment的动作,而且没有调用addToBackStack(),那么当事务提交时,那些被删除的fragment就被销毁了。反之,那些fragment就不会被销毁,而是处于停止状态。当用户返回时,它们会被恢复。

密技:对于fragment事务,你可以应用动画。在commit()之前调用setTransition()就行。――一般银我不告诉他哦。

但是,调用commit()后,事务并不会马上执行。它会在activityUI线程(其实就是主线程)中等待直到线程能执行的时候才执行(废话)。如果必要,你可以在UI线程中调用executePendingTransactions()方法来立即执行事务。但一般不需这样做,除非有其它线程在等待事务的执行。

警告:你只能在activity处于可保存状态的状态时,比如running中,onPause()方法和onStop()方法中提交事务,否则会引发异常。这是因为fragment的状态会丢失。如果要在可能丢失状态的情况下提交事务,请使用commitAllowingStateLoss()

 

// // Copyright (C) 2020 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // package { default_applicable_licenses: [ "Android-Apache-2.0", "vendor_unbundled_google_modules_WifiGooglePrebuilt_license", ], } license { name: "vendor_unbundled_google_modules_WifiGooglePrebuilt_license", visibility: [":__subpackages__"], license_kinds: [ "SPDX-license-identifier-Apache-2.0", "SPDX-license-identifier-BSD", "SPDX-license-identifier-MIT", "SPDX-license-identifier-Unicode-DFS", ], } blueprint_package_includes { match_all: ["com.android.mainline"], } apex_set { name: "com.google.android.wifi", apex_name: "com.android.wifi", owner: "google", overrides: ["com.android.wifi"], filename: "com.google.android.wifi.apex", set: "com.google.android.wifi.apks", prefer: true, use_source_config_var: { config_namespace: "wifi_module", var_name: "source_build", }, exported_bootclasspath_fragments: ["com.android.wifi-bootclasspath-fragment"], exported_systemserverclasspath_fragments: ["com.android.wifi-systemserverclasspath-fragment"], } apex_set { name: "com.google.android.wifi_compressed", apex_name: "com.android.wifi", owner: "google", overrides: ["com.android.wifi"], // filename: "com.google.android.wifi.capex", set: "com.google.android.wifi_compressed.apks", prefer: true, use_source_config_var: { config_namespace: "wifi_module", var_name: "source_build", }, exported_bootclasspath_fragments: ["com.android.wifi-bootclasspath-fragment"], exported_systemserverclasspath_fragments: ["com.android.wifi-systemserverclasspath-fragment"], }
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值