question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mosby 3 MVP - presenter is always recreated when go back to the fragment in the back stack

See original GitHub issue

Hello, 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sockeqwecommented, Aug 17, 2017

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:

@sockeqwe https://github.com/sockeqwe Hi Hannes, have you had a chance to work on the new presenter implementation? Could you share your idea about on how you are going to implement it? If you have a snapshot ready, I’ll be glad to help with testing. Thanks, Thomas

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/sockeqwe/mosby/issues/265#issuecomment-323131223, or mute the thread https://github.com/notifications/unsubscribe-auth/AAjnrqs000n_etDBqxN9niUo1fr3MCEwks5sZG_HgaJpZM4OcZK- .

1reaction
sockeqwecommented, Jul 24, 2017

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:

@sockeqwe https://github.com/sockeqwe Thanks for an explanation about the back stack. If Mosby 3.1 adds adds support for a “queue” it would be great! How would you recommend me implementing my code, until the new 3.1 approach, so it will be easy to migrate?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/sockeqwe/mosby/issues/265#issuecomment-317447597, or mute the thread https://github.com/notifications/unsubscribe-auth/AAjnrlvE91DYTA0IRmgyAtRX8CK9BKQUks5sRK9KgaJpZM4OcZK- .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mosby 3 MVP - presenter is always recreated when go back to ...
When we do popBackStack and return to the previous fragment in the back stack, the bundle in the method onViewCreated(View view, Bundle bundle) ......
Read more >
Changelog of 3.1.0-SNAPSHOT #274 - sockeqwe/mosby
Mosby 3 MVP - presenter is always recreated when go back to the fragment in the back stack #265. Closed. LINT check for...
Read more >
MVP for Android: how to organize the presentation layer
MVP (Model-View-Presenter) is a software design pattern that works pretty well in Android projects and helps separate presentation layer from domain.
Read more >
Navigation: Multiple back stacks - Medium
Intro. Let's say your app uses BottomNavigationView . With this change, when the user selects another tab, the back stack for the current...
Read more >
Fragment related pitfalls and how to avoid them : r/androiddev
A Fragment is created explicitly via your code or recreated implicitly by the FragmentManager. The Fragment Manager can only recreate a Fragment ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found