Android 如何保持横竖屏切换不变以及Fragment调用setRetainInstance的原理

 

限定屏幕方向:
设置Manifest文件的activity的属性:
android:screenOrientation="portrait"(始终竖屏)
landscape(始终横屏)

///////

自己处理变更:
设置Manifest文件中的activity属性:
android:configChanges="orientation|screenSize|keyboardHidden"


视图状态会自动保存(适用于EditText不适用于TextView):
前提条件:
(1)拥有Id
(2)实现onSaveInstanceState回调
(3)实现onRestoreInstanceState回调
 

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d(TAG, "onSaveInstanceState() called with: " + "outState = [" + outState + "]");
        outState.putLong(CREATE_TIME, this.createTime);
        Log.d(TAG, "onSaveInstanceState() called with: " + "outState = [" + outState + "]");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d(TAG, "onRestoreInstanceState() called with: " + "savedInstanceState = [" + savedInstanceState + "]");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: " + this);
    }

 


保持Fragment对象:
(1)在onCreate函数里调用setRetainInstance(true);

setRetainInstance(true);


(2)findFragmentByTag无则创建并添加,有就什么都不做
(2)Fragment对象添加到Activity中,当Activity重启时,通过FragmentManager过去此Fragment对象

    protected void onCreate(Bundle savedInstanceState) {
        private Fragment fragment;
        super.onCreate(savedInstanceState);


        FragmentManager fm = getSupportFragmentManager();

        fragment = fm.findFragmentByTag("ScoreFragment");

        if (fragment == null) {
            fragment = new ScoreFragment();
            fm.beginTransaction().replace(android.R.id.content, fragment, "ScoreFragment").commit();
        }

        Log.d(TAG, "onCreate: ");
    }



保存和恢复Fragment的状态
onSaveInstanceState保持状态
onActivityCreated恢复状态

    public ScoreFragment() {
        // Required empty public constructor
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SCORE, this.score);
        Log.d(TAG, "onSaveInstanceState() called with: " + "outState = [" + outState + "]");
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate() called with: " + "savedInstanceState = [" + savedInstanceState + "]");
//        setRetainInstance(true);

        if (savedInstanceState != null) {
            this.score = savedInstanceState.getInt(SCORE);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: " + this);
    }

setRetainInstance的原理

只有调用了fragment的setRetainInstance(true)方法, 并且因设备配置改变,托管Activity正在被销毁的条件下, fragment才会短暂的处于保留状态。

比较来说,onSaveInstanceState可以更长久的保持数据。 
当Activity所在进程被关闭(非用户主动关闭),系统将会重新创建activity时, 将恢复onSaveInstanceState中保留的数据。

当Fragment设置了setRetainInstance(true)后, 在设备旋转等情况下,该Fragment可以暂时与Activity分离。

如果此时Fragment持有的后台线程,例如AsyncTask中的后台操作等, 需要使用Fragment的Context等信息,就可能出现错误。

为此,在Fragment中定义了isAdded, 用于判断Fragment是否已经绑定到了某个Activity。

你可能感兴趣的:(Android通信开发与应用)