1

THE PROBLEM:

I have one activity with 2 fragment: Fragment1 and Fragment2.

After click one item from my RecyclerView I call Fragment2 with the following line:

fragmentTransaction.add(R.id.frmFragmentItem,detailsFragment);

When the user closes Fragment2 (BackButton or Close button), Fragment1 appears again on the screen.

WHAT I NEED:

Which is the event that is called when the instance of Fragment1 appears again in the screen?

After revise the following life cycle of the fragment, I thought that may be was onCreateView() or onResume() but is not (because the breakpoint didn't stop).

Image

THE QUESTION:

So, could you tell me which event is called when Fragment1 appears again the screen (The instance of the fragment has already created)

ANOTHER POSSIBILITY?

Also I thought to use startActivityForResult & onActivityResult, but I don't know how to use it if I call Fragment2 with the following lines:

Fragment2 detailsFragment = new Fragment2 ();
        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.frmFragmentItem,detailsFragment);
        fragmentTransaction.commit();

Thanks.

Faustino Gagneten
  • 2,564
  • 2
  • 28
  • 54

4 Answers4

2

None of the fragment's lifecycle events will be called, unfortunately, as posted here.

startActivityForResult won't help you when dealing with Fragments.

What you can do is add a listener to the fragment backstack and get informed when fragment 1 is popped from the backstack. You might do that in your Activity, keep references of your Fragment(s), then call some method on the Fragment to inform it that it has "re-appeared".

fweigl
  • 21,278
  • 20
  • 114
  • 205
0
fragmentTransaction.add(R.id.frmFragmentItem,detailsFragment);

will not remove the fragment from container. It still resides in the container that is why you are not receiving the callbacks.

If you use,

fragmentTransaction.replace(R.id.frmFragmentItem,detailsFragment);

the previous fragment will be removed from container and you will receive the proper callbacks as you expected.

Ponsuyambu
  • 7,876
  • 5
  • 27
  • 41
0

After @Ascorbin answer, I could implement what he said:

What you can do is add a listener to the fragment backstack and get informed when fragment 1 is popped from the backstack

I think is important to show how to implement the solution, because on internet there isn't information about it.

For the implementation, you have to do:

1.

Your activity that contains your fragment, has to implement OnBackStackChangedListener like the code below

public class Activity extends AppCompatActivity implements FragmentManager.OnBackStackChangedListener 

2.

Then, in your activity you have to override the listener so:

@Override
public void onBackStackChanged() {
    if(fragment1 != null)
        fragment1.doTheAction();
}

So, when the user comes back from Fragment2 to Fragment1 again, the method onBackStackChanged will be called and Fragment1 will execute doTheAction function.

Finally, when Fragment1 starts Fragment2 I do in my activity the following code:

 Fragment2 secondFragment= new Fragment2();
 android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
 fragmentTransaction.add(R.id.frmFragmentItem,secondFragment);
 fragmentTransaction.addToBackStack("FRAGMENT_1");
 getFragmentManager().addOnBackStackChangedListener(this);
 fragmentTransaction.commit();

After that, you don't need to do anything else.

Nice coding!

Faustino Gagneten
  • 2,564
  • 2
  • 28
  • 54
-1

I know this thread is very old but for people that keep searching about that, you can just override the onResume function from the fragment.

This function is called each time the fragment appears on the user's screen (entering as well as re-entering from forward fragment)