片段中的片段,onRause()在onResume()后调用onPause()

时间:2013-08-02 00:20:27

标签: android android-fragments fragment lifecycle android-lifecycle

在我的应用程序中,我有一个活动,其中包含片段的容器布局。在活动的

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Logger.enable();
    setContentView(R.layout.activity_main);

    FragLobby fragLobby = (FragLobby)getSupportFragmentManager().findFragmentByTag(FragLobby.debugTag);
    if(fragLobby==null) fragLobby = new FragLobby(); 
    FragmentHelper.replaceSupportFragment(this, fragLobby, R.id.llMain, false, FragLobby.debugTag); //helper method, replaces the fragment in a container, false = don't add to back stack


    Logger.log(debugTag, "onCreate");
}

我将片段FragLobby添加到该容器布局中。

在FragLobby的

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    this.root = inflater.inflate(R.layout.frag_lobby, container, false);

    FragPlayerInfo fragPlayerInfo = (FragPlayerInfo)this.getChildFragmentManager().findFragmentByTag(FragPlayerInfo.debugTag);
    if(fragPlayerInfo==null) fragPlayerInfo= new FragPlayerInfo(); 
    FragmentHelper.replaceSupportFragmentInFragment(this, fragPlayerInfo, R.id.rlPlayerInfo, false, FragPlayerInfo.debugTag);

    FragLobbyInfo fragLobbyInfo = (FragLobbyInfo )this.getChildFragmentManager().findFragmentByTag(FragLobbyInfo .debugTag);
    if(fragLobbyInfo==null) fragLobbyInfo= new FragLobbyInfo (); 
    FragmentHelper.replaceSupportFragmentInFragment(this, fragLobbyInfo, R.id.rlLobbyInfo, false, FragLobbyInfo .debugTag); //uses childfragmentmanager


    Logger.log(debugTag, "onCreateView");
    return this.root;
}

帮助函数

public static void replaceSupportFragment(Context ctx, android.support.v4.app.Fragment newFragment, int root, boolean addToBackstack, String tag) {

android.support.v4.app.FragmentManager fman = ((android.support.v4.app.FragmentActivity) ctx).getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ftrans = fman.beginTransaction();
ftrans.setCustomAnimations(R.anim.fade_in, R.anim.fade_out,R.anim.fade_in, R.anim.fade_out);
if(addToBackstack) ftrans.addToBackStack(tag);
ftrans.replace(root,newFragment,tag);
ftrans.commit();
}   

public static void replaceSupportFragmentInFragment(android.support.v4.app.Fragment currentFragment, android.support.v4.app.Fragment newFragment, int root, boolean addToBackstack, String tag) {

android.support.v4.app.FragmentManager fman = currentFragment.getChildFragmentManager();
android.support.v4.app.FragmentTransaction ftrans = fman.beginTransaction();
ftrans.setCustomAnimations(R.anim.fade_in, R.anim.fade_out,R.anim.fade_in, R.anim.fade_out);
if(addToBackstack) ftrans.addToBackStack(tag);
ftrans.replace(root,newFragment,tag);
ftrans.commit();
}

我使用getChildFragmentManager()将另外两个片段添加到我的FragLobby中。 当我运行程序时,我在启动时获得以下生命周期......

08-02 02:00:26.197: I/de.ui.MainActivity(3359): onCreate
08-02 02:00:26.197: I/de.ui.MainActivity(3359): onStart
08-02 02:00:26.342: I/de.ui.FragLobby(3359): onCreate
08-02 02:00:26.347: I/de.ui.FragLobby(3359): onCreateView
08-02 02:00:26.377: I/de.ui.FragPlayerInfo(3359): onCreate
08-02 02:00:26.497: I/de.ui.FragPlayerInfo(3359): onCreateView
08-02 02:00:26.502: I/de.ui.FragLobbyInfo(3359): onCreate
08-02 02:00:26.617: I/de.ui.FragLobbyInfo(3359): onCreateView
08-02 02:00:26.622: I/de.ui.FragLobby(3359): onStart
08-02 02:00:26.622: I/de.ui.FragPlayerInfo(3359): onStart
08-02 02:00:26.622: I/de.ui.FragLobbyInfo(3359): onStart
08-02 02:00:26.622: I/de.ui.FragLobby(3359): onResume
08-02 02:00:26.622: I/de.ui.FragPlayerInfo(3359): onResume
08-02 02:00:26.622: I/de.ui.FragLobbyInfo(3359): onResume
08-02 02:00:26.662: I/de.ui.FragPlayerInfo(3359): onPause
08-02 02:00:26.662: I/de.ui.FragLobbyInfo(3359): onPause
08-02 02:00:26.662: I/de.ui.FragLobby(3359): onPause
08-02 02:00:26.662: I/de.ui.FragPlayerInfo(3359): onStop
08-02 02:00:26.662: I/de.ui.FragLobbyInfo(3359): onStop
08-02 02:00:26.662: I/de.ui.FragLobby(3359): onStop

完成调试日志,当我启动应用程序时会发生这种情况:

08-02 04:16:23.697: V/FragmentManager(17911): Commit: BackStackEntry{41b41a70}
08-02 04:16:23.702: D/FragmentManager(17911):   mName=null mIndex=-1 mCommitted=false
08-02 04:16:23.702: D/FragmentManager(17911):   mEnterAnim=#7f040000    mExitAnim=#7f040001
08-02 04:16:23.702: D/FragmentManager(17911):   mPopEnterAnim=#7f040000     mPopExitAnim=#7f040001
08-02 04:16:23.702: D/FragmentManager(17911):   Operations:
08-02 04:16:23.702: D/FragmentManager(17911):     Op #0: REPLACE FragLobby{41b40c00 id=0x7f090002 de.ui.FragLobby}
08-02 04:16:23.702: D/FragmentManager(17911):   enterAnim=#7f040000 exitAnim=#7f040001
08-02 04:16:23.702: D/FragmentManager(17911):   popEnterAnim=#7f040000 popExitAnim=#7f040001
08-02 04:16:23.702: I/de.ui.MainActivity(17911): onCreate
08-02 04:16:23.702: I/de.ui.MainActivity(17911): onStart
08-02 04:16:23.702: V/FragmentManager(17911): Run: BackStackEntry{41b41a70}
08-02 04:16:23.702: V/FragmentManager(17911): add: FragLobby{41b40c00 id=0x7f090002 de.ui.FragLobby}
08-02 04:16:23.702: V/FragmentManager(17911): Allocated fragment index FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:16:23.702: V/FragmentManager(17911): moveto CREATED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:16:23.722: I/de.ui.FragLobby(17911): onCreate
08-02 04:16:23.722: V/FragmentManager(17911): moveto ACTIVITY_CREATED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:16:23.727: V/FragmentManager(17911): Commit: BackStackEntry{41b49b50}
08-02 04:16:23.727: D/FragmentManager(17911):   mName=null mIndex=-1 mCommitted=false
08-02 04:16:23.727: D/FragmentManager(17911):   mEnterAnim=#7f040000 mExitAnim=#7f040001
08-02 04:16:23.727: D/FragmentManager(17911):   mPopEnterAnim=#7f040000 mPopExitAnim=#7f040001
08-02 04:16:23.727: D/FragmentManager(17911):   Operations:
08-02 04:16:23.727: D/FragmentManager(17911):     Op #0: REPLACE FragPlayerInfo{41b49a80 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.727: D/FragmentManager(17911):   enterAnim=#7f040000 exitAnim=#7f040001
08-02 04:16:23.727: D/FragmentManager(17911):   popEnterAnim=#7f040000 popExitAnim=#7f040001
08-02 04:16:23.727: V/FragmentManager(17911): Commit: BackStackEntry{41b4b478}
08-02 04:16:23.727: D/FragmentManager(17911):   mName=null mIndex=-1 mCommitted=false
08-02 04:16:23.727: D/FragmentManager(17911):   mEnterAnim=#7f040000 mExitAnim=#7f040001
08-02 04:16:23.727: D/FragmentManager(17911):   mPopEnterAnim=#7f040000 mPopExitAnim=#7f040001
08-02 04:16:23.727: D/FragmentManager(17911):   Operations:
08-02 04:16:23.737: D/FragmentManager(17911):     Op #0: REPLACE FragLobbyInfo{41b4b3c0 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:16:23.737: D/FragmentManager(17911):   enterAnim=#7f040000 exitAnim=#7f040001
08-02 04:16:23.737: D/FragmentManager(17911):   popEnterAnim=#7f040000 popExitAnim=#7f040001
08-02 04:16:23.737: I/de.ui.FragLobby(17911): onCreateView
08-02 04:16:23.757: V/FragmentManager(17911): moveto STARTED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:16:23.757: V/FragmentManager(17911): Run: BackStackEntry{41b49b50}
08-02 04:16:23.757: V/FragmentManager(17911): add: FragPlayerInfo{41b49a80 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.757: V/FragmentManager(17911): Allocated fragment index FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.762: V/FragmentManager(17911): moveto CREATED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.762: I/de.ui.FragPlayerInfo(17911): onCreate
08-02 04:16:23.762: V/FragmentManager(17911): moveto ACTIVITY_CREATED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.847: I/de.ui.FragPlayerInfo(17911): onCreateView
08-02 04:16:23.852: V/FragmentManager(17911): Run: BackStackEntry{41b4b478}
08-02 04:16:23.852: V/FragmentManager(17911): OP_REPLACE: adding=FragLobbyInfo{41b4b3c0 id=0x7f09000a de.ui.FragLobbyInfo} old=FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.852: V/FragmentManager(17911): add: FragLobbyInfo{41b4b3c0 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:16:23.852: V/FragmentManager(17911): Allocated fragment index FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:16:23.852: V/FragmentManager(17911): moveto CREATED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:16:23.857: I/de.ui.FragLobbyInfo(17911): onCreate
08-02 04:16:23.857: V/FragmentManager(17911): moveto ACTIVITY_CREATED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:16:23.952: I/de.ui.FragLobbyInfo(17911): onCreateView
08-02 04:16:23.952: I/de.ui.FragLobby(17911): onStart
08-02 04:16:23.952: V/FragmentManager(17911): moveto STARTED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.952: I/de.ui.FragPlayerInfo(17911): onStart
08-02 04:16:23.952: V/FragmentManager(17911): moveto STARTED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:16:23.952: I/de.ui.FragLobbyInfo(17911): onStart
08-02 04:16:23.952: V/FragmentManager(17911): moveto RESUMED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:16:23.952: I/de.ui.FragLobby(17911): onResume
08-02 04:16:23.952: V/FragmentManager(17911): moveto RESUMED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.952: I/de.ui.FragPlayerInfo(17911): onResume
08-02 04:16:23.952: V/FragmentManager(17911): moveto RESUMED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:16:23.952: I/de.ui.FragLobbyInfo(17911): onResume
08-02 04:16:23.977: V/FragmentManager(17911): movefrom RESUMED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:16:23.977: V/FragmentManager(17911): movefrom RESUMED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.977: I/de.ui.FragPlayerInfo(17911): onPause
08-02 04:16:23.977: V/FragmentManager(17911): movefrom RESUMED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:16:23.977: I/de.ui.FragLobbyInfo(17911): onPause
08-02 04:16:23.977: I/de.ui.FragLobby(17911): onPause
08-02 04:16:23.977: V/FragmentManager(17911): movefrom STARTED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:16:23.977: V/FragmentManager(17911): movefrom STARTED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.977: I/de.ui.FragPlayerInfo(17911): onStop
08-02 04:16:23.977: V/FragmentManager(17911): movefrom STARTED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:16:23.977: I/de.ui.FragLobbyInfo(17911): onStop
08-02 04:16:23.977: I/de.ui.FragLobby(17911): onStop
08-02 04:16:23.977: V/FragmentManager(17911): movefrom STOPPED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:16:23.977: V/FragmentManager(17911): movefrom STOPPED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:16:23.977: V/FragmentManager(17911): movefrom STOPPED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}

问题是,为什么所有片段都会立即从开始到停止?活动是活跃的,碎片gui是完全可见的。奇怪的是,当我按下设备上的后退按钮时,我得到以下日志:

08-02 04:18:03.982: V/FragmentManager(17911): moveto STARTED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:18:03.987: I/de.ui.FragLobby(17911): onStart
08-02 04:18:03.987: V/FragmentManager(17911): moveto STARTED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:18:03.987: I/de.ui.FragPlayerInfo(17911): onStart
08-02 04:18:03.987: V/FragmentManager(17911): moveto STARTED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:18:03.987: I/de.ui.FragLobbyInfo(17911): onStart
08-02 04:18:05.222: V/FragmentManager(17911): movefrom STARTED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:18:05.222: V/FragmentManager(17911): movefrom STARTED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:18:05.222: I/de.ui.FragPlayerInfo(17911): onStop
08-02 04:18:05.222: V/FragmentManager(17911): movefrom STARTED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:18:05.222: I/de.ui.FragLobbyInfo(17911): onStop
08-02 04:18:05.222: I/de.ui.FragLobby(17911): onStop
08-02 04:18:05.222: I/de.ui.MainActivity(17911): onStop
08-02 04:18:05.222: V/FragmentManager(17911): movefrom STOPPED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:18:05.222: V/FragmentManager(17911): movefrom STOPPED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:18:05.222: V/FragmentManager(17911): movefrom STOPPED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:18:05.232: V/FragmentManager(17911): movefrom ACTIVITY_CREATED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:18:05.232: V/FragmentManager(17911): movefrom ACTIVITY_CREATED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:18:05.247: V/FragmentManager(17911): movefrom ACTIVITY_CREATED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:18:05.247: V/FragmentManager(17911): movefrom CREATED: FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:18:05.252: V/FragmentManager(17911): movefrom CREATED: FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:18:05.252: I/de.ui.FragPlayerInfo(17911): onDestroy
08-02 04:18:05.252: V/FragmentManager(17911): Freeing fragment index FragPlayerInfo{41b49a80 #0 id=0x7f090009 de.ui.FragPlayerInfo}
08-02 04:18:05.252: V/FragmentManager(17911): movefrom CREATED: FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:18:05.252: I/de.ui.FragLobbyInfo(17911): onDestroy
08-02 04:18:05.252: V/FragmentManager(17911): Freeing fragment index FragLobbyInfo{41b4b3c0 #1 id=0x7f09000a de.ui.FragLobbyInfo}
08-02 04:18:05.252: I/de.ui.FragLobby(17911): onDestroy
08-02 04:18:05.252: V/FragmentManager(17911): Freeing fragment index FragLobby{41b40c00 #0 id=0x7f090002 de.ui.FragLobby}
08-02 04:18:05.262: I/de.ui.MainActivity(17911): onDestroy

它再次开始,只是立即被摧毁。为什么会这样呢?可以做些什么呢?

谢谢:)

1 个答案:

答案 0 :(得分:0)

我实际上可以通过创建一个新项目并像以前一样完成所有事情来解决这个问题。现在的日志输出如下:

08-02 15:36:01.012: I/com.test.FragLobby(25699): onCreate
08-02 15:36:01.017: I/com.test.FragLobby(25699): onCreateView
08-02 15:36:01.022: I/com.test.FragPlayerInfo(25699): onCreate
08-02 15:36:01.077: I/com.test.FragPlayerInfo(25699): onCreateView
08-02 15:36:01.082: I/com.test.FragLobbyInfo(25699): onCreate
08-02 15:36:01.122: I/com.test.FragLobbyInfo(25699): onCreateView
08-02 15:36:01.122: I/com.test.FragLobby(25699): onStart
08-02 15:36:01.122: I/com.test.FragPlayerInfo(25699): onStart
08-02 15:36:01.122: I/com.test.FragLobbyInfo(25699): onStart
08-02 15:36:01.122: I/com.test.FragLobby(25699): onResume
08-02 15:36:01.122: I/com.test.FragPlayerInfo(25699): onResume
08-02 15:36:01.122: I/com.test.FragLobbyInfo(25699): onResume

......正如所料。