android - 遍历 backstack 上的 fragment

标签 android android-fragments back-stack

我发现了很多类似的问题,但我还没有找到任何似乎适合我的具体情况的答案

为了对每个 fragment 执行特定操作,迭代返回堆栈中所有 fragment 的正确方法是什么?我需要根据环境的变化更新一些 fragment 并删除一些 fragment (理论上,我可以在 fragment 再次获得焦点时捕获一个事件,然后采取适当的行动,但这会使事情变得有点复杂,因为我'我也在处理重命名的事情)

假设 fragment 的容器如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/fragment_container"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" />

并添加 fragment 如下:

FragmentTransaction transaction = context.getSupportFragmentManager().beginTransaction();
Fragment fragment = new CustomFragment();

transaction.add(R.id.fragment_container, fragmentBase);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.addToBackStack(nameOfFragment);

transaction.commit();

context.getSupportFragmentManager().executePendingTransactions();

我第一次尝试迭代是:

getSupportFragmentManager().executePendingTransactions();
for (int indexFragment = 0; indexFragment < getSupportFragmentManager().getBackStackEntryCount(); indexFragment++)
{
    FragmentManager.BackStackEntry backStackEntry = getSupportFragmentManager().getBackStackEntryAt(indexFragment);                    

    // If Android keeps the ID, surely there should be a way of getting to the actual fragment itself, from that ID?
    Fragment fragment = getSupportFragmentManager().findFragmentById(backStackEntry.getId());                   

    if (fragment != null)
    {               
        //TODO: Cast the fragment and perform some transactions against it

        // We never get here; as fragment is always null
    }
}

换个角度:

FrameLayout frameLayout = (FrameLayout)findViewById(R.id.fragment_container);

for (int indexFrameLayout = 0; indexFrameLayout < frameLayout.getChildCount(); indexFrameLayout++)
{
    View view = (View)frameLayout.getChildAt(indexFrameLayout);

    // I get the right number of views; but I can't interact with them as fragments
}

假设我可以通过保留对 fragment 的引用来解决这个问题,但这种方法存在问题。

  • 有没有办法获取当前在后台堆栈中的 fragment ?

  • 假设我可以访问一个 fragment ,是否有办法将它(并且单独地)从后台堆栈中的某处移除? (有 transaction.remove 方法,但它是为了处理位于后台堆栈中间的 fragment 吗?)

谢谢

最佳答案

BackStackEntry 不必与单个 Fragment 相关联,它代表一个 FragmentManager 状态,您可能在事务中有一堆 fragment 并且因此一堆 fragment 处于一种状态。使用 this flavor add() 方法并通过标签为您的 fragment 分配一个唯一的 id。然后您就可以通过 getSupportFragmentManager().findFragmentById(fragment_tag);

找到它们

至于你的任务,afaik 没有办法从 BackStack 中删除任意状态,你唯一的选择是弹出堆栈,从顶部一个接一个地删除状态。因此,例如,您可以覆盖 onBackPressed() 并调用 fragmentManager.popBackStack() 以简单地转到之前的状态或 fragmentManager.popBackStack(backstack_tag) 跳过一些状态并转到您需要的地方。

关于android - 遍历 backstack 上的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20451737/

相关文章:

android - YouTube Android API 停止工作

android - View 在 ViewPager 中的 Fragment 中混淆了 EditText 焦点

android - 当使用不同的 Intent 启动 Activity 时,如何防止 Activity 的多个实例

java - 我正在尝试将 junit 与 apt 插件一起使用

java - 每当我使用改造从网络访问数据时,它工作正常,但第二次无法使用改造通过网络找到数据

android - Google Play 是否有一些 "Feature Graphic"生成器?

android - 将监听器设置为来自 Fragment 的 Android 服务

android - 静态 fragment

android - 更改 Android 上 Activity 动画的 z 顺序

android - 如果 Activity 进入画中画模式,则不会调用 onActivityResult