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.
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);
}
@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,
- When button is clicked, LoginFragment gets replaced with
HomeFragment,
onPause()
of LoginFragment, andonResume()
of HomeFragment gets called - When back is pressed, HomeFragment is poped out and LoginFragment is
seen, and
onPause()
of HomeFragment andonResume()
of LoginFragment gets called.
What I am getting is,
- When button is clicked, HomeFragment is correctly replacing LoginFragment, onResume() of HomeFragment is called, but onPause() of LoginFragment is never called.
- When back pressed, HomeFragment is correctly popping to reveal LoginFragment, onPause() of HomeFragment gets called, but onResume() of LoginFragment never gets called.
Is this the normal behaviour? Why is onResume()
of LoginFragment not getting called when I press the back button.
ft2.replace()
,FragmentTransaction.remove()
method is called and theLoginfragment
will be removed. Refer to this. SoonStop()
ofLoginFragment
will be called instead ofonPause()
. (As the new fragment completely replaces the old one).ft2.addtobackstack()
, the state of theLoginfragment
will be saved as a bundle and when you click back button fromHomeFragment
,onViewStateRestored()
will be called followed byonStart()
ofLoginFragment
. So eventuallyonResume()
won't be called.If you really want to replace fragment inside other fragment you should use Nested Fragments.
In your code you should replace
with
What i do in child fragment:
And then override onResume on ParentFragment
onPause()
method works in activity class you can use:for same purpose..
A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments
If you add the fragment in XML, you can't swap them dynamically. What happens is they overly, so they events don't fire as one would expect. The issue is documented in this question. FragmenManager replace makes overlay
Turn middle_fragment into a FrameLayout, and load it like below and your events will fire.