Support getting saved state directly
Kisty opened this issue · comments
Thanks for this library. I'm currently trying to fix issues with ViewPager containing large amount of pages and not using a state saving library so doing it by hand as suggested in #14.
Is there a way to get
the Bundle
stored by Bridge? I use the state in onCreate()
to restore the underlying data which for whatever reason couldn't be used in onRestoreInstanceState()
therefore I can't refactor very easily the code to use just restoreState(Bundle)
. Could you point me in the direction I'd need to go if I just wanted the Bundle
, please?
Thanks!
Hi @Kisty . I'm trying to see if I follow the question : is the problem that using restoreState
appears to be asynchronous while you need to access the data synchronously somehow? After Bridge.restoreInstanceState
is called, the data should be supplied immediately to your target. So while there is no method like Bridge.getData(...)
that would return a Bundle
directly, you should be able to act like you have all your data the moment Bridge.restoreInstanceState
is called. You could, for example, just save the Bundle
passed to restoreState
in a member variable if that's what you are looking for:
public class MainActivity extends Activity implements StateManager {
@Nullable
private Bundle mSavedState = null;
...
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.create(savedInstanceState);
Bridge.restoreInstanceState(this, savedInstanceState);
if (mSavedState != null) {
// This should be available whenever savedInstanceState is not null
//
// ...do whatever you need to do with the Bundle...
}
}
@Override
public void restoreState(@Nullable Bundle bundle) {
mSavedState = bundle;
}
...
}
Let me know if that helps address your question or if there is something I'm missing.
Thanks @byencho. That's spot on. Why didn't I think of that! I'll give it a whirl.
Can I also put the entire Android view state in Bridge or is that not supported? I see that I can supply the ViewSavedStateHandler
but that won't support built-in Android Views or did I miss the point of it.
Well, that's solved all my problems. Turns out it does store the Android View state. Thanks again!
Apologies, it doesn't store the view state. Is this supported at all?
@Kisty Glad to help!
As for the Android view state, for Activities and Fragments Bridge
is designed to only store the custom data you give it. The rest of the saved state (like the Fragment stack, the view hierarchy state, etc.) are stored in the original saved state Bundle
undisturbed.
As you mention, you can use a ViewSavedStateHandler
along with the Bridge methods for saving state in views to save custom state directly in a View as well. That will actually save the normal "parent state" of the View (ex: TextView text, position, etc.) along with any custom data you supply. You can take a look at LargeDataView in the sample app to see how that works.
So depending on your problem that may or may not help you out. If you have some more information on what the issue is you are having with view state maybe I could give you a little more firm of an answer one way or the other.