单实例活动Android的碎片生命周期 [英] Android Fragment Lifecycle of Single Instance Activity

查看:130
本文介绍了单实例活动Android的碎片生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个singleInstance活动和片段,我实例中的onCreate()方法,并添加到的FrameLayout容器放入活动的布局。活动什么都不做,除了打印出日志。 我现在用的是Android系统的支持-V4 lib和安卓2.3.3。

I have a singleInstance Activity and a Fragment which I instantiate in the onCreate() method and add into a FrameLayout container into the layout of the activity. The activity does nothing except printing out the logs. I am using the android-support-v4 lib and android 2.3.3.

我观察这个设置一个陌生的生命周期行为,我不知道你会帮我解释这一点。我将提供日志的生命周期:

I observed a strange lifecycle behavior with this setting and I wonder if you might help me to explain this. I will provide the logs for the lifecycles:

该活动的第一次调用:

    07-07 15:12:17.990 V/FragActivity( 2358): onCreate >> com.test.fragmentlife.FragActivity@44f98778
    07-07 15:12:21.010 V/FragActivity( 2358): onCreate <<
    07-07 15:12:21.020 V/LayoutFragment( 2358): onAttach > LayoutFragment{44f467c8 #0 id=0x7f070000}
    07-07 15:12:24.021 V/LayoutFragment( 2358): onAttach <
    07-07 15:12:24.021 V/LayoutFragment( 2358): onCreate > LayoutFragment{44f467c8 #0 id=0x7f070000}
    07-07 15:12:27.020 V/LayoutFragment( 2358): onCreate <
    07-07 15:12:27.020 V/LayoutFragment( 2358): onCreateView > LayoutFragment{44f467c8 #0 id=0x7f070000}
    07-07 15:12:30.022 V/LayoutFragment( 2358): onCreateView <
    07-07 15:12:30.030 V/LayoutFragment( 2358): onActivityCreated > LayoutFragment{44f467c8 #0 id=0x7f070000}
    07-07 15:12:33.030 V/LayoutFragment( 2358): onActivityCreated <
    07-07 15:12:33.030 V/LayoutFragment( 2358): onStart > LayoutFragment{44f467c8 #0 id=0x7f070000}
    07-07 15:12:36.030 V/LayoutFragment( 2358): onStart <
    07-07 15:12:36.040 V/FragActivity( 2358): onStart > com.test.fragmentlife.FragActivity@44f98778
    07-07 15:12:39.041 V/FragActivity( 2358): onStart <
    07-07 15:12:39.041 V/LayoutFragment( 2358): onStop > LayoutFragment{44f467c8 #0 id=0x7f070000}
    07-07 15:12:42.040 V/LayoutFragment( 2358): onStop <
    07-07 15:12:42.040 V/FragActivity( 2358): onResume > com.test.fragmentlife.FragActivity@44f98778
    07-07 15:12:45.041 V/FragActivity( 2358): onResume <
    07-07 15:12:45.041 V/LayoutFragment( 2358): onStart > LayoutFragment{44f467c8 #0 id=0x7f070000}
    07-07 15:12:48.040 V/LayoutFragment( 2358): onStart <
    07-07 15:12:48.040 V/LayoutFragment( 2358): onResume > LayoutFragment{44f467c8 #0 id=0x7f070000}
    07-07 15:12:51.042 V/LayoutFragment( 2358): onResume <

第一个问题:为什么是片段的onStop()创建活动的过程中的方法?该片段在屏幕上显示的罚款。

First question: why is the onStop() method of the fragment during the creation of the activity? The Fragment displays fine on the screen.

之后,我重新启动该活动由烧制的意图,导致活动onNewIntent()lidecycle方法。

After that I restart the activity by firing an intent, causing the onNewIntent() lidecycle method of the activity.

    07-07 15:13:17.220 V/LayoutFragment( 2358): onPause > LayoutFragment{44f467c8 #0 id=0x7f070000}
    07-07 15:13:20.220 V/LayoutFragment( 2358): onPause <
    07-07 15:13:20.230 V/FragActivity( 2358): onPause > com.test.fragmentlife.FragActivity@44f98778
    07-07 15:13:23.231 V/FragActivity( 2358): onPause <
    07-07 15:13:23.231 V/FragActivity( 2358): onNewIntent > com.test.fragmentlife.FragActivity@44f98778
    07-07 15:13:26.231 V/FragActivity( 2358): onNewIntent <
    07-07 15:13:26.231 V/FragActivity( 2358): onResume > com.test.fragmentlife.FragActivity@44f98778
    07-07 15:13:29.230 V/FragActivity( 2358): onResume <

第二个问题:为什么是onResume片段()方法不叫?它仍然显示在屏幕上。据我了解活动和生命周期方法应当齐头并进......

Second question: why is the onResume() method of the fragment not called? It is still visible on the screen. As far as i know activity and lifecycle methods should go hand in hand...

之后,我重新启动活动第二次:

After that i restart the activity a second time:

    07-07 15:13:42.140 V/FragActivity( 2358): onPause > com.test.fragmentlife.FragActivity@44f98778
    07-07 15:13:45.143 V/FragActivity( 2358): onPause <
    07-07 15:13:45.143 V/FragActivity( 2358): onNewIntent > com.test.fragmentlife.FragActivity@44f98778
    07-07 15:13:48.144 V/FragActivity( 2358): onNewIntent <
    07-07 15:13:48.150 V/FragActivity( 2358): onResume > com.test.fragmentlife.FragActivity@44f98778
    07-07 15:13:51.151 V/FragActivity( 2358): onResume <

现在的fragmen的生命周期方法,不会触发所有..怎么会这样?

Now the lifecycle methods of the fragmen are not triggered at all.. how is that?

推荐答案

我不能回答的问题1,虽然我已经注意到这种行为为好,但我很少做大量的工作在我的onStop方法(我喜欢的onPause和onResume),我可以帮助你的第二个问题。

I can't answer question 1, although I've noticed that behavior as well, but I rarely do much work in my onStop methods (I favor onPause and onResume), I can help with your 2nd question.

这里的问题(这绝对是一个谷歌的bug)要么是一个与FragmentActivity问题或活动的生命周期作为一个整体(取决于你如何看待它)。

The problem here (which is definitely a google bug) is either a problem with FragmentActivity or the Activity lifecycle as a whole (depending on how you look at it).

基本上,FragmentActivity移动它的片段到续状态,而不是在onResume方法(作为一个一般理智的人可能会认为),但在onPostResume方法。这是一切都很好,除了onPostResume方法永远不会被调用的singleIntstance / singleTask活动时,该活动回顾与onNewIntent。

Basically, the FragmentActivity moves its fragments to the resumed state, not in the onResume method (as a generally sane person might think) but in the onPostResume method. This is all well and good, except that the onPostResume method never gets called in a singleIntstance/singleTask activity when the activity is recalled with onNewIntent.

所以,我没有(有进口的,而不是仅仅的jar支持包code,)是修改FragmentActivity像这样...

So what I did (having imported the support package code, instead of just the jar) was modify FragmentActivity like so...

//this boolean is only ever set to true in onNewIntent
private boolean mResumeNeedsToDoDispatch = false; 

/**
 * Ensure any outstanding fragment transactions have been committed.
 */
@Override
protected void onResume() {
    super.onResume();
    mResumed = true;

    //Check if onNewIntent was called. If so, dispatch resumes to fragments now
    if (mResumeNeedsToDoDispatch) {
        mResumeNeedsToDoDispatch = false;
        mFragments.dispatchResume();
    }

    mFragments.execPendingActions();
}


/**
 * Google, I think you forgot this #(
 */
@Override
public void onNewIntent(Intent newIntent) {
    super.onNewIntent(newIntent);
    mResumeNeedsToDoDispatch = true;
}

请注意,我不只是调用mFragments.dispatchResume()在onNewIntent导致这会导致片段onResume方法,在这种情况下被调用两次。我不是100%肯定这是为什么,但是这就是我发现在我的测试。

Notice that I didn't just call mFragments.dispatchResume() in onNewIntent cause this causes the fragment onResume methods to be called twice in this case. I'm not 100% sure why that is, but that is what I noticed in my tests.

希望这有助于:)

这篇关于单实例活动Android的碎片生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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