ViewPager 和 Fragment 生命周期与 Activity [英] ViewPager and Fragment lifecycle with Activity

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

问题描述

我正在使用一个 Activity,它有一个 ViewPager 保存 2 个片段,寻呼处理程序是 FragmentPagerAdapter 的一些实现.

I'm using an Activity which has a ViewPager holding 2 fragments, the pager handler is some implementation of FragmentPagerAdapter.

据我所知,pager 适配器处理其中的片段的生命周期.

As I understand, pager adapter handles the lifecycle of the fragments inside it.

  • 我发现我的 Activity onResume() 方法已经被调用,但片段 onStart() 方法甚至没有启动.我怎么能解决这个问题?它破坏了活动和片段之间的整个生命周期交互点...

  • I found out that my Activity onResume() method already gets called but the fragment onStart() method didn't even started. how in the world can I fix that? it destroy the whole point of lifecycle interactions between activity an fragments...

既然 pager 适配器处理 Fragment 的生命周期,这是否意味着我不能再依赖与 Activity 的交互?我的意思是,如果我希望 ActivityonResume() 中但在 Fragment onStart() 之后做一些事情> 被调用了,我就是做不到...

Since pager adapter handles the lifecycle of the Fragment, does this means I can no longer depend on interaction with the Activity? I mean, if I want the Activity to do something in the onResume() but after the Fragment onStart() is called, I just can't do it...

为了清楚起见:谷歌表示 Activity 和 Fragment 的生命周期是一起进行的,一旦一个被调用,另一个也被调用,例如

To make things clear: Google says lifecycle of activity and fragment are going together, once one gets called, the other also gets called, e.g

  1. Activiy -> onCreate() ,然后,Fragment -> onCreate()

  1. Activiy -> onCreate() , and then, Fragment -> onCreate()

Activiy -> onResume() ,然后,Fragment -> onResume()

Activiy -> onResume() , and then, Fragment -> onResume()

但是!就我而言,我得到:Activity -> onCreate() -> onStart() -> onResume() -> onPostResume()

BUT! in my case I get: Activity -> onCreate() -> onStart() -> onResume() -> onPostResume()

然后:Fragment -> onAttach() -> onCreateView() -> ... ->onResume().

And then: Fragment -> onAttach() -> onCreateView() -> ... ->onResume().

需要明确的是,我使用的是寻呼机适配器(不是状态"寻呼机),并且我的应用程序中有一个抽象的基础活动,所有活动都应该扩展.

and to be clear, I am using a pager adapter (not "state" pager) and I have an abstract base activiy in my app which all activities should extend.

public abstract class AbsLoginAppCompatActivity extends AppCompatActivity {
.............
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "*******************onCreate");
    //do some general stuff like check for updates on server
}

在我的扩展活动中:

public class A extends AbsLoginAppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e(TAG, "*******************onCreate");
    setContentView(R.layout.activity_a);
    //also set pager + adapter + give it getSupportFragmentManager()
}

我正在使用:

android.support.v4.view.ViewPager

android.support.v4.app.FragmentPagerAdapter

android.support.v4.app.Fragment

android.support.v7.app.AppCompatActivity(用于 abs 活动)

android.support.v7.app.AppCompatActivity (for abs activity)

推荐答案

Fragment[State]PagerAdapter 使用活动 FragmentManager - 或者在嵌套 ViewPager 在父片段中 - 该片段的子 FragmentManager 管理片段,就像普通片段一样.实际上,这些适配器实现所做的唯一一件事就是为您隐藏了令人讨厌的 FragmentTransaction 内容.

The Fragment[State]PagerAdapter uses the activities FragmentManager - or in case of a nested ViewPager in a parent fragment - that fragment's child FragmentManager to manage the fragments, just like normal fragments would do. Really, the only thing that these adapter implementations do is that they hide the nasty FragmentTransaction stuff for you.

我从来没有遇到过在我的片段中没有为我调用特定生命周期回调的问题,所以我对此无话可说.然而,有一点很重要,但很多人都弄错了,适配器的 getItem() 方法仅在新创建片段时调用;如果它从保存状态恢复,则不会再次调用此方法,人们倾向于在那里做所有花哨的事情来初始化他们刚刚创建"的片段,而他们应该真正研究 instantiateItem(),其中要么返回您通过 getItem() 提供给适配器的实例,要么返回自动为您重新创建的片段的引用.

I had never problems that particular lifecycle callbacks weren't called for me in my fragments, so I cannot say anything about that. One thing however that is important to understand and that many people get wrong is that the adapter's getItem() method is called only when a fragment is freshly created; if it is restored from a saved state this method is not called again and people tend to do all fancy things there to initialize their just "created" fragment, while they should really look into instantiateItem(), which either returns the instance you give the adapter via getItem() or returns the reference of the fragment that was automatically re-created for you.

关于 pager 中的片段的另一件事是了解 setUserVisibleHint(boolean) 方法.由于片段通常是一次性(非状态适配器)或按需(状态适配器)重新创建和恢复的,因此了解一个实例何时对用户真正可见通常很重要.这可以通过在自定义片段中覆盖上述方法来实现.

Another thing that is good to know about fragments in pager is the method setUserVisibleHint(boolean). Since fragments are usually recreated and resumed all at once (non-state adapter) or on demand (state adapter), its usually important to know when one instance is actually visible to the user. This can be achieved by overriding the aforementioned method in a custom fragment.

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

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