片段 onResume() &onPause() 不会在 backstack 上调用 [英] Fragment onResume() & onPause() is not called on backstack

查看:26
本文介绍了片段 onResume() &onPause() 不会在 backstack 上调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个活动中有多个片段.单击按钮后,我开始创建一个新片段,并将其添加到 backstack.我自然希望当前 Fragment 的 onPause() 方法和 new Fragment 的 onResume() 方法被调用.好吧,它没有发生.

I have multiple fragment inside an activity. On a button click I am starting a new fragment, adding it to backstack. I naturally expected the onPause() method of current Fragment and onResume() of new Fragment to be called. Well it is not happening.

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

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

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

我所期望的是,

  1. 点击按钮时,LoginFragment 被替换为HomeFragmentLoginFragmentonPause()onResume()HomeFragment 被调用
  2. 按下返回键时,HomeFragment 会弹出,LoginFragment 是看到了,HomeFragmentonPause()LoginFragmentonResume()被调用.
  1. When button is clicked, LoginFragment gets replaced with HomeFragment, onPause() of LoginFragment, and onResume() of HomeFragment gets called
  2. When back is pressed, HomeFragment is poped out and LoginFragment is seen, and onPause() of HomeFragment and onResume() of LoginFragment gets called.

我得到的是,

  1. 单击按钮时,HomeFragment 正在正确替换LoginFragmentHomeFragment的onResume()被调用,但是onPause()LoginFragment 永远不会被调用.
  2. 按下后,HomeFragment 正确弹出以显示LoginFragmentHomeFragment 的 onPause() 被调用,但是 onResume()LoginFragment 永远不会被调用.
  1. When button is clicked, HomeFragment is correctly replacing LoginFragment, onResume() of HomeFragment is called, but onPause() of LoginFragment is never called.
  2. When back pressed, HomeFragment is correctly popping to reveal LoginFragment, onPause() of HomeFragment gets called, but onResume() of LoginFragment never gets called.

这是正常行为吗?为什么当我按下后退按钮时没有调用 LoginFragmentonResume().

Is this the normal behaviour? Why is onResume() of LoginFragment not getting called when I press the back button.

推荐答案

onResume()onPause() 片段只会在活动 时被调用onResume()onPause() 被调用.它们与 Activity 紧密耦合.

The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.

阅读本文的处理片段生命周期部分.

这篇关于片段 onResume() &onPause() 不会在 backstack 上调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆