Moxy icon indicating copy to clipboard operation
Moxy copied to clipboard

After restarting the activity from the stack, presenters of its fragments are created earlier than the presenter of the activity itself

Open Anrimian opened this issue 6 years ago • 2 comments

How did I come to this:

There is activity, there are many nested fragments in it. Activity has a presenter, each fragment also has a presenter. Activity launch another activity that falls (why - it does not matter in this matter). After crashing, the system recreates the activity stack and starts the previous activity with a bunch of fragments.

However, the activity has the starting data, which lies in the intent. Passing these data to fragments through arguments is long and painful (there are a lot of them, they are nested in each other) - so I pass them through di (I use dagger2). The presenter of the activity is in the component, the presenters of the fragments are in the subcomponents of the activity component. And under this order of creation, the subcomponent tries to create before its parent component, as a result, everything flies to hell.

This answer explains the behavior, there is no turning of the device, because the presenters are not created when turning, but the situation is similar.

Yes, of course, the application should work stably and without crashes. However, in this situation, I get crash from moxy in analytics(fabric), and I do not see the root causes of crashes(maybe problem in analytics, i don't know). It is also possible that there are other ways to get this behavior.

How I solved the problem:

I created my own MvpActivity, where I called getMvpDelegate().onCreate() before calling super.onCreate(savedInstanceState).

I think this behavior should be in the library by default.

Anrimian avatar Apr 28 '18 07:04 Anrimian

Hi, @Anrimian

Sorry for my delay =)

I think that there may be some navigation problems if users change navigation stack from presenter. And than it will change fragments back stack before activity recreate this state. And there may be some problems.

I think your should make your data layer more flexible and restore them without depends on some activity or other lifecycle components. Did you?

senneco avatar Aug 08 '18 17:08 senneco

I did not use the fragments backstack here. It is just activity with very heavy ui. And i describe very-very rarely case(restore activity after crash, this normally never happens), usually everything works fine. But it is weird, when in this case fragments presenters creates before activity presenter.

Anrimian avatar Aug 09 '18 07:08 Anrimian