的生命周期更换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,其中有2 ListFragments。这些ListFragments 正在创建的ViewPager的OnCreate事件。
  • 当您单击ListFragments的一个项目,它是由不同的片段更换整个ViewPagerFragment(这是另一个ListFrament)。
  • I have an application showing some tabs. One of those tab is showing a ViewPager, in which there is two ListFragments. Those ListFragments are create in the onCreate event of the ViewPager.
  • When you click an item in one of the ListFragments, it is replacing the entire ViewPagerFragment by a different fragment (which is another a ListFrament).

下面是我用它来与新的ListFragment

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在外面)。
  • ViewPager is reloading fine (from onCreateView), but the ListFragment inside won't show up. Look like my ListFragment's object are still alive, but it seems they lost their view, and onCreateView is not called to recreate it (because the object didn't slept while ViewPager was out).

我不张贴更多的code在这里了,因为我的目的是要知道,如果我想要做的是确定或没有思想的机器人的方式。请问我要对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)。这就是为什么我很迷惑。

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和谷歌播放确实有导航,看起来像我的一点点(更换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天全站免登陆