Android 在屏幕上保存自定义对象在片段或活动中旋转

Android 在屏幕上保存自定义对象在片段或活动中旋转,android,android-fragments,android-activity,android-lifecycle,android-savedstate,Android,Android Fragments,Android Activity,Android Lifecycle,Android Savedstate,我知道这个问题很常见,我读过很多不同的答案,但没有一个适合我的问题。在我的应用程序中,我有一个活动,在rhye活动中,我加载一个片段。我还将一些数据(以捆绑的形式)发送到片段。因此,我的问题是,当屏幕旋转时,我将片段保存在onSaveInstanceState活动方法中,并检入onCreate方法weather savedInstance是否为null,然后在此基础上加载片段。 活动代码: @Override public void onSaveInstanceState(Bundle out

我知道这个问题很常见,我读过很多不同的答案,但没有一个适合我的问题。在我的应用程序中,我有一个活动,在rhye活动中,我加载一个片段。我还将一些数据(以捆绑的形式)发送到片段。因此,我的问题是,当屏幕旋转时,我将片段保存在
onSaveInstanceState
活动方法中,并检入onCreate方法weather savedInstance是否为null,然后在此基础上加载片段。 活动代码:

 @Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    outState.putParcelable(Const.TAG_REQ_CUSTOM,DetailsItems);
    outState.putString(Const.TAG_FLOW, Const.TAG_MAIN_FLOW);
    getSupportFragmentManager().putFragment(outState,"current_fragment",fragment);
}
onCreate方法:

if (findViewById(R.id.fragment_frame) != null) {
        if (savedInstanceState != null) {
// this invoke when screen rotate but the app crash 

            DetailsItems = savedInstanceState.getParcelable(Const.TAG_REQ_CUSTOM);
            String flow = savedInstanceState.getString(Const.TAG_FLOW);
           ft = getSupportFragmentManager().getFragment(savedInstanceState,"current_fragment");
            mFragmentManager=getSupportFragmentManager();
            mFragmentTransaction = mFragmentManager.beginTransaction();
            bundle= new Bundle();
            bundle.putString(Const.TAG_FLOW, flow);
            bundle.putParcelable(Const.TAG_REQ_BOOKING_DETAILS, bookingDetailsItems);
            ft.setArguments(bundle);
            mFragmentTransaction.replace(R.id.fragment_frame, ft).commit();
        }else{
           // load fragment on first time
        }
    }
所以我的问题是:我必须在哪里保存自定义对象(在父活动中还是在片段中)? 当我保存的实例不为空且应用程序崩溃且日志为:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
看一看

从文档:

当知道将立即为新配置创建一个新实例时,作为由于配置更改而销毁活动的一部分,由系统调用。您可以在这里返回您喜欢的任何对象,包括活动实例本身,稍后可以通过在新活动实例中调用getLastNonConfigurationInstance()来检索该对象


在活动中使用此代码:

    if (findViewById(R.id.fragment_frame) != null) {
        if (savedInstanceState != null) {
fragment =getSupportFragmentManager().getFragment(savedInstanceState,"current_fragment");

        }else{
           // load fragment on first time
        }
    }
碎片:

//save custom object

 @Override
    public void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        outState.putParcelable("key",customObject);
    }

//now retrieve here

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null)
    customObject= savedInstanceState.getParcelable("key");
}
你应该使用
ViewModel
就是专门为此而设计的

从文档中:

ViewModel是一个类,负责准备和管理活动或片段的数据。它还处理活动/片段与应用程序其余部分的通信(例如,调用业务逻辑类)


从docs链接可以看到,这些方法是在
活动中定义的。这假设对象实现了
Parcelable
接口,而OP询问如何保存/恢复任意对象。