Fragment onResume() & onPause() is not called on backstack

10/14 08:08
阅读数 0

问题:

I have multiple fragment inside an activity.我在一个活动中有多个片段。 On a button click I am starting a new fragment, adding it to backstack.单击按钮后,我将开始一个新片段,并将其添加到 backstack。 I naturally expected the onPause() method of current Fragment and onResume() of new Fragment to be called.我自然期待当前 Fragment 的onPause()方法和 new Fragment 的onResume()方法被调用。 Well it is not happening.嗯,它没有发生。

LoginFragment.java登录片段.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 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);
  }

  @Override
  public void onResume() {
     Log.e("DEBUG", "onResume of HomeFragment");
     super.onResume();
  }

  @Override
  public void onPause() {
     Log.e("DEBUG", "OnPause of HomeFragment");
     super.onPause();
  }
}

What I expected, was,我所期望的是,

  1. When button is clicked, LoginFragment gets replaced with HomeFragment , onPause() of LoginFragment , and onResume() of HomeFragment gets called当用户点击按钮,LoginFragment获取与HomeFragment,取而代之onPause() LoginFragment的,和onResume() HomeFragment的被调用
  2. When back is pressed, HomeFragment is poped out and LoginFragment is seen, and onPause() of HomeFragment and onResume() of LoginFragment gets called.当被压回,HomeFragment是POP操作并LoginFragment可见,并onPause() HomeFragmentonResume() LoginFragment的被调用。

What I am getting is,我得到的是,

  1. When button is clicked, HomeFragment is correctly replacing LoginFragment , onResume() of HomeFragment is called, but onPause() of LoginFragment is never called.当用户点击按钮,HomeFragment正确更换LoginFragment,的onResume()的HomeFragment被调用,但在onPause(LoginFragment的)永远不会被调用。
  2. When back pressed, HomeFragment is correctly popping to reveal LoginFragment , onPause() of HomeFragment gets called, but onResume() of LoginFragment never gets called.当按下回来,HomeFragment正确弹出透露LoginFragment,在onPause()的HomeFragment被调用,但的onResume(LoginFragment的)不会被调用。

Is this the normal behaviour?这是正常行为吗? Why is onResume() of LoginFragment not getting called when I press the back button.当我按下后退按钮时,为什么没有调用LoginFragment 的onResume()


解决方案:

参考一: https://en.stackoom.com/question/lWRv
参考二: https://stackoom.com/question/lWRv
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部