一. 强制开启屏幕旋转效果
三.onSaveInstanceState(Bundle outState)方法的默认实现
案例:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//你的后续操作,下文有举例
}
四.是否需要重写onSaveInstanceState(Bundle outState)方法
书写案例:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("message", text.getText().toString());
outState.putCharSequence("text", text.getText());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
message = savedInstanceState.getString("message");
text = savedInstanceState.getCharSequence("text");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
CharSequence text = savedInstanceState.getCharSequence("text");
text.setText(charSequence);
}
}
09-01 20:58:43.386 1649-1649/com.jxnu.duewang.day05 E/main: onCreate
09-01 20:58:43.610 1649-1649/com.jxnu.duewang.day05 E/main: onStart
09-01 20:58:43.611 1649-1649/com.jxnu.duewang.day05 E/main: onResume
09-01 20:58:51.789 1649-1649/com.jxnu.duewang.day05 E/main: onPause//开始转屏
09-01 20:58:51.791 1649-1649/com.jxnu.duewang.day05 E/main: onSaveInstanceState
09-01 20:58:51.791 1649-1649/com.jxnu.duewang.day05 E/main: onstop
09-01 20:58:51.791 1649-1649/com.jxnu.duewang.day05 E/main: onRetainCustomNonConfigurationInstance
09-01 20:58:51.791 1649-1649/com.jxnu.duewang.day05 E/main: ondestory
09-01 20:58:51.841 1649-1649/com.jxnu.duewang.day05 E/main: onCreate
09-01 20:58:51.897 1649-1649/com.jxnu.duewang.day05 E/main: onStart
09-01 20:58:51.897 1649-1649/com.jxnu.duewang.day05 E/main: getLastCustomNonConfigurationInstance
09-01 20:58:51.897 1649-1649/com.jxnu.duewang.day05 E/main: onRestoreInstanceState
09-01 20:58:51.897 1649-1649/com.jxnu.duewang.day05 E/main: onResume
案例:
@Override
public Object onRetainCustomNonConfigurationInstance() {
Log.e(TAG,"onRetainCustomNonConfigurationInstance");
CharSequence text = this.text.getText();
return text;
}
@Override//可以直接在onCreate中获取,写在这里只是一个示范
public Object getLastCustomNonConfigurationInstance() {
String str=super.getLastCustomNonConfigurationInstance().toString();
return str;
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
text.setText((CharSequence) getLastCustomNonConfigurationInstance());
Log.e(TAG,"onRestoreInstanceState");
}
六.横屏竖屏的判断
//判定当前的屏幕是竖屏还是横屏
public int ScreenOrient(Activity activity)
{
int orient = activity.getRequestedOrientation();
if(orient != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE && orient != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
WindowManager windowManager = activity.getWindowManager();
Display display = windowManager.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
orient = screenWidth < screenHeight ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
return orient;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//强行开启屏幕旋转效果
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
if(savedInstanceState == null){
setContentView(R.layout.activity_orientation1);
}
if(savedInstanceState != null){
//横屏
if( ScreenOrient(this)==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE )
setContentView(R.layout.activity_orientation1);
//竖屏
if( ScreenOrient(this)==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT )
setContentView(R.layout.activity_orientation2);
}
}