在 Fragment 或 Activity 中将自定义对象保存在屏幕上旋转

Save custom object on screen rotate in Fragment or Activity

我知道这个问题很常见,我已经阅读了很多不同的答案,但 none 适合我的问题。在我的应用程序中,我有一个 activity 并且在 rhye activity 中我加载了一个片段。我还向片段发送了一些数据(以 Bundle 的形式)。所以我的问题是当屏幕旋转时,我将片段保存在 onSaveInstanceState Activity 方法中并检查 onCreate 方法天气 savedInstance 是否为 null 并在此基础上加载片段。 Activity代码:

 @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
        }
    }

所以我的问题是:我必须在哪里保存自定义对象(在父 Activity 或片段中)? 当我保存的实例不为 null 而不是应用程序 crashesh 并且日志为:

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

看看onRetainNonConfigurationInstance() and getLastNonConfigurationInstance()

来自文档:

Called by the system, as part of destroying an activity due to a configuration change, when it is known that a new instance will immediately be created for the new configuration. You can return any object you like here, including the activity instance itself, which can later be retrieved by calling getLastNonConfigurationInstance() in the new activity instance.

在 Activity 中使用此代码:

    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");
}

你应该使用 ViewModelViewModel 是专门为此目的而制作的。

来自文档:

ViewModel is a class that is responsible for preparing and managing the data for an Activity or a Fragment. It also handles the communication of the Activity / Fragment with the rest of the application (e.g. calling the business logic classes).