0

When i add fragment and go to Fragment2 from Fragment1 with this code:

Bundle bundle = new Bundle();
                    bundle.putString(Constants.USER_NAME, item_username);
                    AppCompatActivity activity = (AppCompatActivity) view.getContext();
                    Fragment myFragment = new Fragment2();
                    myFragment.setArguments(bundle);
                    activity.getSupportFragmentManager().beginTransaction().add(R.id.frame_fragment_containers, myFragment).addToBackStack(null).commit();

I can't check onPause and onDestroy in Fragment1 and onResume in Fragment1 when i come back from Fragment2. I want do this with add and don't use replace. Please help me. Thanks

  • Does this answer your question? [Fragment onResume() & onPause() is not called on backstack](https://stackoverflow.com/questions/11326155/fragment-onresume-onpause-is-not-called-on-backstack) – hosseinAmini Mar 22 '20 at 15:54

2 Answers2

0

onPause() and onResume() are for activities. I think you should try setting visibility of the fragment by:

public class YourFragment extends Fragment {
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
           // Do Something onResume
        }
        else {
           // Do Something onPause
        }
    }
}
Mike
  • 1,313
  • 12
  • 21
0

You need to use replace() instead of add() in order for fragment1 to be added to the back stack and fragment2 to be shown.

Shahood ul Hassan
  • 745
  • 2
  • 9
  • 19
  • When i use replace, kill my previous fragment and when in fragment2 press back button, reload fragment1. So i want use add in fragment – Mohammadreza Mar 23 '20 at 11:10
  • `replace()` doesn't 'kill' your fragment1, it destroys its view and recreates it when you press back button while in fragment2. You shouldn't keep on adding fragments one after the other. That's not the way to go. If `replace()` is creating problems for u in some way, u better review ur overall code once again. – Shahood ul Hassan Mar 23 '20 at 11:53
  • Please help me so I can replace fragment without destroy fragment1 view and when press back button in fragment2, don't reload fragment1. Thanks – Mohammadreza Mar 23 '20 at 12:23
  • Let me know why destroying of fragment view is an issue for u. – Shahood ul Hassan Mar 23 '20 at 13:05
  • In Fragment1 i have video player, and i want when go to Fragment2, pause video in Fragment1, and when destroy Fragment2 and go to Fragment1, resume video in Fragment1 without recreate. – Mohammadreza Mar 23 '20 at 13:33
  • What you can do is to save the status of video player, `Uri` of video and its current position, if playing, in `onSaveInstanceState()` of fragment1 and retrieve it in `onCreateView()` of fragment1 when u restore it back. If the retrieved state of video player tells u that a video was playing when view was destroyed, u can resume it using the retrieved position. – Shahood ul Hassan Mar 23 '20 at 13:41
  • I have list of videos like instagram and play videos automatically in list and I don't want recreate Fragment1 view when i am in Fragment2 and press back button. – Mohammadreza Mar 23 '20 at 13:53
  • It is very difficult to suggest something useful until u describe ur use case in a clear manner. – Shahood ul Hassan Mar 23 '20 at 15:44