0

I'm using MVP in my project. I have an activity. Inside this activity I'm showing fragment. Fragment contains some data, edit text, buttons etc. From this fragment it is possible to navigate to another fragment. Here is my code for showing another fragment:

 getParentFragmentManager()
                .beginTransaction()
                .replace(R.id.main_container, secondFragment)
                .addToBackStack(null)
                .commitAllowingStateLoss();

Next, when I try to go back from fragment 2 to fragment 1, my fragment one is re-created.The method onViewCreated() is called again with saveedInstanceState = null. Also in the onViewCreated() I call presenter.onCreate(savedInstanceState) and because of this, all requests that should be called when I first enter the fragment are re-called when I back to first fragment.

As far as I understand, when calling a .replace(container, fragment), I cannot avoid recreating the fragment view, but in this case, how can I save all my data in the presenter so that I do not re-execute all requests when returning to the fragment?

P.S. With .add() fragment is not recreated. But in my activity I have toolbar with title, and I don't need it to show in my second fragment. When I open my second fragment with .replace() toolbar is not showing, but when open with .add() toolbar is showing.

testivanivan
  • 967
  • 13
  • 36

1 Answers1

0

Use Fragment Result API : For implemention this method set - setFragmentResultListener - in source fragment and also set - setResult() and finish() - in destination fragment

Note 1 : for having clean code i suggest you to use FragmentTransaction class in:

https://github.com/mahditavakoli1312/FragmentTransaction---mahdi-tavakoli

Note 2 : use Navigation for navigate betwin fragments and activities Instead tranastion