Mosby 3 MVP - presenter is always recreated when go back to the fragment in the back stack
See original GitHub issueHello, I discribed the problem on stackoverflow first, but didn’t get the response so far. So, ask my question here.
When we do popBackStack and return to the previous fragment in the back stack, the bundle in the method onViewCreated(View view, Bundle bundle) is always null due to the fact that onSaveInstanceState(Bundle outState) wasn’t called before. So, when we go back, bundle is null and a presenter (and view state) is created again. How in this case we can reuse presenter and view state (and not recreate it)?
You can see a dummy example below. There is a fragment with 2 buttons. One button opens a new Fragment and another button goes to the previous one. When you go back, presenter and view states are recreated. That’s not I need, but I described above why it’s happenning according to the code in the library. Is there a way to ensure that we reuse presenter and view state when we go back?
public class FirstFragment extends MvpViewStateFragment<FirstFragmentView, FirstFragmentPresenter, FirstFragmentViewState> {
public static final String TAG = "TAG";
private Button moveToAnotherFragmentButton;
private Button moveBackButton;
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
final View rootFragmentView = inflater.inflate(R.layout.first_fragment, container, false);
moveToAnotherFragmentButton = (Button) rootFragmentView.findViewById(R.id.first_fragment_go_to_another_fragment_button);
moveBackButton = (Button) rootFragmentView.findViewById(R.id.first_fragment_back_button);
return rootFragmentView;
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
moveToAnotherFragmentButton.setOnClickListener(ignored -> addToStack(FirstFragment.class));
moveBackButton.setOnClickListener(ignored -> getFragmentManager().popBackStack());
}
@Override
@NonNull
public FirstFragmentPresenter createPresenter() {
Log.e(TAG, "createPresenter");
return new FirstFragmentPresenter();
}
@NonNull
@Override
public FirstFragmentViewState createViewState() {
Log.e(TAG, "createViewState");
return new FirstFragmentViewState();
}
@Override
public void onNewViewStateInstance() {
Log.e(TAG, "onNewViewStateInstance");
}
private void addToStack(@NonNull final Class<? extends Fragment> fragmentClass) {
final FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager.isDestroyed()) {
throw new UnexpectedException("FragmentManager is destroyed");
}
final Fragment fragment = Fragment.instantiate(getContext(), fragmentClass.getName());
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.replace(R.id.activity_main_content_container, fragment, null)
.addToBackStack(null)
.commit();
}
}```
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:8 (7 by maintainers)
Top GitHub Comments
Hi, sorry, not yet. Most likely I have time at end August to work on that…
On Do., 17. Aug. 2017, 18:52 Thomas Vos notifications@github.com wrote:
Since this is mostly yet another Presenter implementation I try to find some time for it this week. Its not like moxy which needs an entirely framework and tools like annotation processing around it.
Thomas Vos notifications@github.com schrieb am Mo., 24. Juli 2017, 16:50: