片段onResume()及在onPause()不叫上backstack [英] Fragment onResume() & onPause() is not called on backstack

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

问题描述

我有一个活动内的多个片段。在点击一个按钮,我开始一个新的片段,将其添加到backstack。我自然希望当前片段的的onPause()方法和 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();
  }
}

我所期待的,是,

What I expected, was,

  1. 点击按钮, LoginFragment 被替换 HomeFragment 的onPause() LoginFragment onResume() HomeFragment 被称为
  2. 在当前背面是pressed, HomeFragment 被弹出了和 LoginFragment 是 可见,和的onPause() HomeFragment onResume() LoginFragment 被调用。
  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 正确更换 LoginFragment ,onResume(中) HomeFragment 被调用,但在onPause() 对 LoginFragment 永远不会被调用。
  2. 当回pressed, HomeFragment 正确弹出来揭示 LoginFragment 的,在onPause() HomeFragment 被调用,但是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.

这是正常的行为呢?为什么 onResume() LoginFragment 没有得到所谓的,当我preSS后退按钮。

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

推荐答案

片段 onResume()的onPause()将被称为只有当活动 onResume()的onPause()被调用。 他们紧密地耦合到活动

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

阅读处理这篇文章的片段生命周期部分。

Read the Handling the Fragment Lifecycle section of this article.

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

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