0

I have Fragment A. On clicking some button, i add Fragment B above Fragment A.

    FragmentB fragmentB= new FragmentB ();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.content_home, fragmentB, "fragmentB");
    fragmentTransaction.hide(FragmentA.this);
    fragmentTransaction.addToBackStack(FragmentA.class.getName());
    fragmentTransaction.commit();

I perform some action in Fragment B. After finishing my work i popBackStack() current fragment.

  FragmentManager   fragmentManager = getFragmentManager();
  fragmentManager.popBackStack();

Now i Fragment A. Since i used "ADD fragment" all my values are kept in edittext.

I want to know, which method is invoked when i come back to Fragment A, is it onResume()..? I have to put log in onResume, seems like it not going to onResume!

Darshn
  • 1,512
  • 1
  • 23
  • 31
  • 1
    **setuservisiblehint** method will be called whenever the fragment become visible to the user – Rahul Khurana Oct 28 '16 at 04:57
  • @RahulKhurana i will try this and let you know! :) – Darshn Oct 28 '16 at 05:00
  • @RahulKhurana http://stackoverflow.com/a/20657022/3348373 according to this link, setUserVisibleHint() is called in fragments put in a pager.! I override setUserVisibleHint() and it wasn't called – Darshn Oct 28 '16 at 05:19
  • Have a look at this [fragment lifecycle] ( https://lh3.googleusercontent.com/-ePcpvCCqKgY/UhODSSrFHoI/AAAAAAAAAoM/uq6Y9aWvh-cLGoao9DFy5K6zJ-NZzrDIA/w506-h930/android_fragment_lifecycle_complete.png ) – Rahul Khurana Oct 28 '16 at 05:31

2 Answers2

0

instead of fragmentTransaction.add(R.id.content_home, fragmentB, "fragmentB"); use

fragmentTransaction.replace(R.id.content_home, fragmentB, "fragmentB");

refer link Difference between add(), replace(), and addToBackStack()

try below code

FragmentB fragmentB= new FragmentB ();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.content_home, fragmentB, "fragmentB");
    fragmentTransaction.hide(FragmentA.this);
    fragmentTransaction.addToBackStack(FragmentA.class.getName());
    fragmentTransaction.commit();
Community
  • 1
  • 1
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

According to Fragment documentation, your fragment will be resumed after pressing back. https://developer.android.com/guide/components/fragments.html

if you do call addToBackStack() when removing a fragment, then the fragment is stopped and will be resumed if the user navigates back.

But you said,

I have to put log in onResume, seems like it not going to onResume!

Then you may have some problems in your code:
- If you do

fragmentTransaction.hide(FragmentA.this);

then you may need to use FragmentTransaction.show(Fragment), in order to display FragmentA again. And then it will go to onResume().

giang nguyen
  • 393
  • 4
  • 12