片段onResume()和onPause()在后台程序中未调用

我在一个活动中有多个片段。在一个按钮上,单击“我正在开始一个新的片段,将其添加到堆栈”。我自然希望可以调用onPause()当前Fragment和onResume()new Fragment 的方法。好吧,这没有发生。


LoginFragment.java

public class LoginFragment extends Fragment{

  @Override

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

      final View view  =   inflater.inflate(R.layout.login_fragment, container, false);

      final FragmentManager mFragmentmanager =  getFragmentManager();


      Button btnHome  = (Button)view.findViewById(R.id.home_btn);

      btnHome.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view){

           HomeFragment fragment    = new HomeFragment();

           FragmentTransaction ft2   =  mFragmentmanager.beginTransaction();

           ft2.setCustomAnimations(R.anim.slide_right, R.anim.slide_out_left

                    , R.anim.slide_left, R.anim.slide_out_right);

           ft2.replace(R.id.middle_fragment, fragment);

           ft2.addToBackStack(""); 

           ft2.commit();    

         }

      });

  }


  @Override

  public void onResume() {

     Log.e("DEBUG", "onResume of LoginFragment");

     super.onResume();

  }


  @Override

  public void onPause() {

    Log.e("DEBUG", "OnPause of loginFragment");

    super.onPause();

  }

}

HomeFragment.java

public class HomeFragment extends Fragment{

  @Override

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

     final View view  =   inflater.inflate(R.layout.login_fragment, container, false);

  }

我所期望的是


当用户点击按钮,LoginFragment被替换为 HomeFragment,onPause()的LoginFragment和onResume()的 HomeFragment被调用

当被压回,HomeFragment是POP操作并LoginFragment看到,和onPause()的HomeFragment和onResume()的LoginFragment 被调用。

我得到的是


当用户点击按钮,HomeFragment正确更换 LoginFragment,的onResume()的HomeFragment被调用,但在onPause(的)LoginFragment永远不会被调用。

当按下回来,HomeFragment正确弹出透露 LoginFragment,在onPause()的HomeFragment被调用,但的onResume(的)LoginFragment不会被调用。

这是正常行为吗?为什么当我按下“后退”按钮时onResume(),LoginFragment没有被调用。

片段onResume()和onPause()在后台程序中未调用

隔江千里
浏览 742回答 3
3回答

至尊宝的传说

片段onResume()或onPause()仅在调用活动onResume()或时才onPause()被调用。他们紧密地联系在一起Activity。

回首忆惘然

这是我的Gor答案的更可靠的版本(使用fragments.size()是不可靠的,因为在弹出片段后大小不会减小)getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onBackStackChanged() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (getFragmentManager() != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fragment topFrag = NavigationHelper.getCurrentTopFragment(getFragmentManager());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (topFrag != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (topFrag instanceof YourFragment) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //This fragment is being shown.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Navigating away from this fragment.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });和“ getCurrentTopFragment”方法:public static Fragment getCurrentTopFragment(FragmentManager fm) {&nbsp; &nbsp; int stackCount = fm.getBackStackEntryCount();&nbsp; &nbsp; if (stackCount > 0) {&nbsp; &nbsp; &nbsp; &nbsp; FragmentManager.BackStackEntry backEntry = fm.getBackStackEntryAt(stackCount-1);&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; fm.findFragmentByTag(backEntry.getName());&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; List<Fragment> fragments = fm.getFragments();&nbsp; &nbsp; &nbsp; &nbsp; if (fragments != null && fragments.size()>0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Fragment f: fragments) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (f != null && !f.isHidden()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return f;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return null;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android