恢复/带到活动(但隐藏)片段的顶部 [英] Resume/bring to top active (but hidden) fragment

查看:59
本文介绍了恢复/带到活动(但隐藏)片段的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用

String name = "fragment1"; // and ..2 and ..3
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content_frame, fragment, name);
fragmentTransaction.addToBackStack(name);
fragmentTransaction.commit();

最后添加的(第三个)片段现在在顶部可见.现在,我想恢复到第一个添加的片段.但是如何?我可以用

The last added (third) Fragment now is visible on top. Now I want to resume to the first added Fragment. But how? I can find this Fragment with

FragmentManager fragmentManager = getSupportFragmentManager();
Fragment firstFragment = fragmentManager.findFragmentByTag("fragment1");

如果我调用 fragmentManager.getFragments(),我仍然可以找到所有三个片段.

If I call fragmentManager.getFragments() I still can find all three Fragments.

如何使 firstFragment 返回顶部,使其再次可见?

How to bring firstFragment back to top, make it visible again?

推荐答案

您可以隐藏您的 2nd 3rd 片段,并使您的第一个片段可见.因此,您将具有第一个片段显示在顶部而其他片段不可见的效果.

You can hide your 2nd and 3rd fragment and make your 1st fragment visible. So you'll have the effect that first fragment is shown on top and others are invisible.

解决方案:

使用 FragmentTransaction的show and hide方法.首先,您需要找到所有片段并调用 FragmentTransaction 来显示和隐藏第二个和第三个片段.

Use the FragmentTransaction's show and hide method. Firs you need to find all the fragment and call the FragmentTransaction to show and hide 2nd and 3rd fragments.

这篇关于恢复/带到活动(但隐藏)片段的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆