0

I have a situation where onDestroy fires immediately after onSaveInstanceState.

Will the runtime environment (dalvik) retain a copy of the bundle set in onSaveInstanceState, and pass it back into onCreate?

Or, is the bundle null/void? If the bundle does end up being destroyed, is persistence of the "view"/"gui"-state to be done instead in onPause (or some other lifetime method)?


Update (specific problem driving my question)

public class CView : MXFragmentView<CViewVM>
{     
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
    {
        base.OnCreateView(inflater, container, bundle);

        _inflater = inflater;
        _container = container;
        _bundle = bundle;       

        ...
    }   

    public override void OnSaveInstanceState(Bundle outState)
    {                
        Log.Info(TAG_APP, "CView.OnSaveInstanceState(): BEGIN");
        base.OnSaveInstanceState(outState);

        outState.PutInt(SaveStateParams.CurrentTabIndex, _tabHost.CurrentTab);
    }

    public override void OnDestroy()
    {
        Log.Info(TAG_APP, "CView.OnDestroy(): BEGIN");
        base.OnDestroy();
    }
}

I/APP_TAG(6398): CView.OnSaveInstanceState(): BEGIN
I/APP_TAG(6398): CView.OnDestroy(): BEGIN
W/Bundle(6398): Key android:view_state expected Bundle but value was a android.util.SparseArray.  The default value <null> was returned.
W/Bundle(6398): Attempt to cast generated internal exception:
W/Bundle(6398): java.lang.ClassCastException: android.util.SparseArray cannot be cast to android.os.Bundle
W/Bundle(6398):     at android.os.Bundle.getBundle(Bundle.java:1142)
W/Bundle(6398):     at android.app.LocalActivityManager.dispatchCreate(LocalActivityManager.java:455)
W/Bundle(6398):     at CView.n_onCreateView(Native Method)
W/Bundle(6398):     at CView.onCreateView(CView.java:60)

I've been familiarizing with post Saving Android Activity state

Community
  • 1
  • 1
samus
  • 6,102
  • 6
  • 31
  • 69
  • No it won't. The saved state will be passed to `onCreate()` when the `Activity` is recreated. – Xaver Kapeller Feb 07 '17 at 16:44
  • @XaverKapeller That's great news, however I'm currently dealing with the ladder (I'm getting a SparseArray/Bundle cast exception). – samus Feb 07 '17 at 16:46
  • Cast exception? Then you must be doing something wrong. I can't tell you what the problem is without seeing the stacktrace of the exception and your code, please edit them into your question. – Xaver Kapeller Feb 07 '17 at 16:48
  • It happens when an activity is re-created and the bundle passed into onCreate is a SparseArray, which apparently occurs when the bundle isn't saved in onSaveInstanceState (it's like the default/null value or something). – samus Feb 07 '17 at 16:50
  • Please just show me your code and the stacktrace, otherwise I can't really help you aside from guessing what you might be doing wrong. – Xaver Kapeller Feb 07 '17 at 16:52
  • @XaverKapeller I updated my post with code and exception. Your time and help is much appreciated. – samus Feb 07 '17 at 17:06
  • 1
    `Will the runtime environment (dalvik) retain a copy of the bundle set in onSaveInstanceState, and pass it back into onCreate?`. No. Not when onDestroy() has triggered. – greenapps Feb 07 '17 at 17:17
  • @greenapps Good to know. If ya have a suggestion for storing the view state that the bundle was supposed to (in onPause I think), it would be cool if you could add that to your comment for an answer. – samus Feb 07 '17 at 17:22

1 Answers1

0

W/Bundle(6398): at android.os.Bundle.getBundle(Bundle.java:1142)

Arrr, sohdat's where dis devil be castin'!

public final class Bundle implements Parcelable, Cloneable {
    private static final String LOG_TAG = "Bundle";
    public static final Bundle EMPTY;
    ...
    Map<String, Object> mMap = null;
    ...
    public Bundle getBundle(String key) {
        unparcel();
        Object o = mMap.get(key);
        if (o == null) {
            return null;
        }
        try {
            return (Bundle) o;
        } catch (ClassCastException e) {
        typeWarning(key, o, "Bundle", e);
        return null;
    }
    ...
}
samus
  • 6,102
  • 6
  • 31
  • 69