被替换的 ViewPager 和 BackStack 的生命周期? [英] Lifecycle of a replaced ViewPager and BackStack?

本文介绍了被替换的 ViewPager 和 BackStack 的生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的对 android ViewPager 的生命周期感到困惑,而且我开始认为我的应用程序结构有问题.因此,如果我可以执行以下操作,我想确认一下:

I'm really messed up with the android ViewPager's lifecycle, and I'm starting to think that there something wrong in the structure of my app. So I would like a confirmation if I can do the following :

  • 我有一个应用程序显示了一些选项卡.其中一个选项卡显示一个 ViewPager,其中有两个 ListFragment.那些 ListFragments在 ViewPager 的 onCreate 事件中创建.
  • 当您单击其中一个 ListFragment 中的项目时,它会将整个 ViewPagerFragment 替换为另一个片段(这是另一个 ListFrament).

这是我用来用新的 ListFragment 替换 ViewPager 的代码

Here is the code that I use to replace the ViewPager with the new ListFragment

FragmentManager fm = getFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();

ft.replace(R.id.container, new RequestForm(requestsList.get(itemIndex)));
ft.addToBackStack(null);
ft.commit();

从那里开始,一切正常.但是,如果用户单击后退按钮,则会出现以下情况:

From there, everything is working fine. But, if the user clicks the back button, I get the following situation:

  • ViewPager 重新加载正常(来自 onCreateView),但 ListFragment里面不会出现.看起来我的 ListFragment 对象还在活着,但似乎他们失去了视野,而 onCreateView 不是调用以重新创建它(因为对象在ViewPager 出局了).

我现在不会在这里发布更多代码,因为我的目的是想知道我正在尝试做的事情是否符合 Android 的思维方式.我是否必须与 Android SDK 对抗才能存档我想做的事情?

I'm not posting more code here for now, because my intention is to know if what I'm trying to do is ok or not in an Android way of thinking. Will I have to fight against Android SDK to archive what I want to do?

推荐答案

好吧,如您所见,我不是 Android 用户(我对 iOS 上的 UI 了解得更多).这就是我困惑的原因.

Well, as you can see, I'm not an Android User (I know much more about UI on iOS). This is why I was confuse.

在查看不同的应用程序后,我发现 Gmail 和 Google Play 的导航功能与我的有点相似(用新片段替换 ViewPager,而不创建新活动).

After looking a different applications, I found out that Gmail and Google Play do have navigation that looks a little bit like mine (replacing a ViewPager by a new fragment, without creating a new activity).

对于我的问题,这不是结构问题.创建 FragmentStatePagerAdapter 时,请务必使用 getChildFragmentManager() 而不是 getFragmentManager()!

And for my problem, it wasn't a structure problem. When you create the FragmentStatePagerAdapter, be sure to use getChildFragmentManager() and not getFragmentManager()!

ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

这篇关于被替换的 ViewPager 和 BackStack 的生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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