Save object while orientation change

后端 未结 4 1600
栀梦
栀梦 2021-02-02 07:20

How to save Object while orientation change, since onRetainNonConfigurationInstance and getLastNonConfigurationInstance are deprecated. And which cannot me used with compa

相关标签:
4条回答
  • 2021-02-02 07:59

    There are two alternatives:

    1. Use a Loader. The FragmentActivity will take care of saving/restoring its state when re-creating.
    2. Use a fragment without a view and call setRetainInstance(true) on it. There is an example of this in the compatibility library's source, FragmentRetainInstanceSupport or some such.
    0 讨论(0)
  • 2021-02-02 07:59

    You can use onRetainCustomNonConfigurationInstance.

    Use this instead of onRetainNonConfigurationInstance(). Retrieve later with getLastCustomNonConfigurationInstance().

    0 讨论(0)
  • 2021-02-02 08:02

    When your Fragment is paused it will call this method:

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Add variable to outState here
        super.onSaveInstanceState(outState);
    }
    

    The variable outState will then be fed into the onCreate() method when the Fragment restarts.

    You can save any data that is a basic type or implements Parcelable interface

    0 讨论(0)
  • 2021-02-02 08:02

    Well, quote from Android Developers' References:

    This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

    Regarding the use of onSaveInstanceState(), it's better to revert your objects/things by using onRestoreInstanceState() instead.

    Refer to Activity#onRestoreInstanceState()

    0 讨论(0)
提交回复
热议问题