1

I'm using Mosby Model-View-Presenter library in an Android app. In one particular view I'm using Bottom Navigation implemented with Design Support Library v25.1.0. I'm switching between 3 fragments in an Activity. For handling rotation configuration change I'm using Mosby's custom RestorableViewState.

The problem I have is that Mosby is storing View's state in Activity's onSaveInstanceState(Bundle bundle) which is NOT called when I swap fragments in the same activity, only Fragment's onDestroyView() is being called. Currently I'm storing the state separately in the Fragment itself (since the fragment is not destroyed during switching fragments, just the Fragment's view) but I have redundant code which kinda defeats the purpose of using MVP architecture.

Is there any more elegant solution to store Fragment's state during both orientation change AND swapping fragments?

4

1 回答 1

1

This is a little bit tricky. I guess you are making a FragmentTransaction with replace() aren't you? The problem is that with replace you are really creating a new Fragment instance every time. Hence no state that can be restored. You should rather call FragmentTransaction.show(fragment) and FragmentTransaction.hide(fragment).

Also Mosby 2 doesn't have prefect support for this usecase, Mosby 3 will have better support for that.

于 2017-01-23T18:01:58.153 回答