onDestroy是否使onSaveInstanceState包无效?

时间:2017-02-07 16:40:19

标签: android

我有一种情况, onDestroy 会在 onSaveInstanceState 之后立即触发。

运行时环境(dalvik)是否会在 bundle 中保留 onSaveInstanceState 的副本,并将其传回 onCreate

或者,是 bundle null / void? 如果捆绑包最终为 destroyed ,那么“view”/“gui”状态的持久性将在 onPause 中完成(或其他一些生命方法)?

更新(驱动我的问题的具体问题)

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)

我一直熟悉帖子Saving Android Activity state

1 个答案:

答案 0 :(得分:0)

  

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

Arrr,sohdat在哪里扮演魔鬼&#39;!

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;
    }
    ...
}