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.
You simple can't add a fragment to a fragment. This needs to happen in the FragmentActivity. I assume you are creating the LoginFragment in a FragmentActivity, so in order to get this working you need to add the HomeFragment via the FragmentActivity when the login closes.
The general point is that you need a FragmentActivity class from where you add each Fragment to the FragmentManager. It is not possible to do this inside a Fragment class.
The fragments
onResume()
oronPause()
will be called only when the ActivitiesonResume()
oronPause()
is called. They are tightly coupled to theActivity
.Read the Handling the Fragment Lifecycle section of this article.
I have a code very similar to yours and if it works onPause () and onResume (). When changing the fragment, these functions are activated respectively.
Code in fragment:
Log when change of fragment:
Fragment onCreateView:
Action when I pulse back (in the activity where I load the fragment):
You can try this,
Step1: Override the Tabselected method in your activity
Step 2: Using static method do what you want in your fragment,
Here's my more robust version of Gor's answer (using fragments.size()is unreliable due to size not being decremented after fragment is popped)
And the 'getCurrentTopFragment' method: