本文介绍了片段片段的onPause()onResume之后被称为()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在我的应用我有一个容器布局片段的单一活动。在活动的

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

我的片段FragLobby添加到容器布局。

I add the fragment FragLobby to that container layout.

在FragLobby的

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

辅助功能

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

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

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

完整DEBUGLOG,出现这种情况时,我启动应用程序:

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}

现在的问题是,为什么所有的碎片瞬间从开始到停止?活性是活动的,所述片段桂是完全可见的。自叹不如,当我preSS装置,我得到以下日志上的后退按钮:

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?

感谢:)

推荐答案

我其实只是创建一个新的项目,正是因为之前做的一切解决这个问题。现在日志输出中如下所示:

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

...预期。

这篇关于片段片段的onPause()onResume之后被称为()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:25