Android横竖屏的写法、及savedInstanceState的使用

我们的横竖屏布局文件:

Android横竖屏的写法、及savedInstanceState的使用_第1张图片

 

竖屏XML:




    

横屏XML:




    

    

我们的java文件,主要演示saveInstanceState的使用:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    String TAG = "buder";

    TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = findViewById(R.id.text);

        //方式一:数据恢复,从K-V中获取销毁之前的存储值
        if (savedInstanceState != null) {
            String test = savedInstanceState.getString("hehe");
            Log.e(TAG, "onCreateRestore" + test);
            mTextView.setText(test);
        }
    }

    //在pause/stop之间进行界面上的数据存储,到K-V中
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e(TAG, "onSaveInstanceState");
        String change = mTextView.getText().toString();
        outState.putString("hehe", change);
    }

    //方式二:数据恢复,从K-V中获取销毁之前的存储值
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        String test = savedInstanceState.getString("hehe");
        Log.e(TAG, "onRestore" + test);
        mTextView.setText(test);
    }
}

项目地址:https://github.com/buder-cp/base_component_learn/tree/master/instancestatetest

你可能感兴趣的:(Android)