问题描述
我有一个活动内的多个片段。在点击一个按钮,我开始一个新的片段,将其添加到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,
- 点击按钮, LoginFragment 被替换 HomeFragment ,
的onPause()
的 LoginFragment 和onResume()
的 HomeFragment 被称为 - 在当前背面是pressed, HomeFragment 被弹出了和 LoginFragment 是可见,和
的onPause()
的 HomeFragment 和onResume()
的 LoginFragment 被调用。
- When button is clicked, LoginFragment gets replaced withHomeFragment,
onPause()
of LoginFragment, andonResume()
ofHomeFragment gets called - When back is pressed, HomeFragment is poped out and LoginFragment isseen, and
onPause()
of HomeFragment andonResume()
of LoginFragmentgets called.
我所得到的是,
- 点击按钮, HomeFragment 正确更换 LoginFragment ,onResume(中) HomeFragment 被调用,但在onPause()对 LoginFragment 永远不会被调用。
- 当回pressed, HomeFragment 正确弹出来揭示 LoginFragment 的,在onPause() HomeFragment 被调用,但是onResume()对 LoginFragment 永远不会被调用。
- When button is clicked, HomeFragment is correctly replacingLoginFragment, onResume() of HomeFragment is called, but onPause()of LoginFragment is never called.
- When back pressed, HomeFragment is correctly popping to revealLoginFragment, 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!