Note that there are some explanatory texts on larger screens.

plurals
  1. POFragment onResume() & onPause() is not called on backstack
    primarykey
    data
    text
    <p>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 <code>onPause()</code> method of current Fragment and <code>onResume()</code> of new Fragment to be called. Well it is not happening.</p> <h2>LoginFragment.java</h2> <pre><code>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(); } } </code></pre> <h2>HomeFragment.java</h2> <pre><code>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(); } } </code></pre> <p>What I expected, was,</p> <ol> <li>When button is clicked, <strong>LoginFragment</strong> gets replaced with <strong>HomeFragment</strong>, <code>onPause()</code> of <strong>LoginFragment</strong>, and <code>onResume()</code> of <strong>HomeFragment</strong> gets called</li> <li>When back is pressed, <strong>HomeFragment</strong> is poped out and <strong>LoginFragment</strong> is seen, and <code>onPause()</code> of <strong>HomeFragment</strong> and <code>onResume()</code> of <strong>LoginFragment</strong> gets called.</li> </ol> <p>What I am getting is,</p> <ol> <li>When button is clicked, <strong>HomeFragment</strong> is correctly replacing <strong>LoginFragment</strong>, onResume() of <strong>HomeFragment</strong> is called, but onPause() of <strong>LoginFragment</strong> is never called.</li> <li>When back pressed, <strong>HomeFragment</strong> is correctly popping to reveal <strong>LoginFragment</strong>, onPause() of <strong>HomeFragment</strong> gets called, but onResume() of <strong>LoginFragment</strong> never gets called.</li> </ol> <p>Is this the normal behaviour? Why is <code>onResume()</code> of <strong>LoginFragment</strong> not getting called when I press the back button.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload