Fragment in Fragment, onPause() called right after

2019-07-24 16:26发布

in my app I have a single activity with a container layout for a fragment. In Activity's

@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");
}

I add the fragment FragLobby to that container layout.

In FragLobby's

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;
}

helper functions

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();
}

I add two other fragments to my FragLobby, using getChildFragmentManager(). When I run the program, I get the following lifecycle at startup...

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

Complete debuglog, this happens when I start the app:

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}

The question is, why do all the fragments instantly go from start to stop? The activity is active, the fragments gui is completely visible. Strange enough, when I press the back button on the device I get the following log:

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

It gets started again, just to be instantly destroyed. Why is it that way and what can be done about it?

Thanks :)

1条回答
地球回转人心会变
2楼-- · 2019-07-24 17:24

I could actually fix this just by creating a new project and doing everything EXACTLY as before. Now the log ouput is as follows:

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

...as expected.

查看更多
登录 后发表回答