Activity生命周期-切换横竖屏-Android

Posted hequnwang10

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activity生命周期-切换横竖屏-Android相关的知识,希望对你有一定的参考价值。

创建一个项目,测试横竖屏切换的时候生命周期的变换
ScreenActivity.java

public class ScreenActivity extends AppCompatActivity 

    private static final String TAG = "ScreenActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen);
        Log.i(TAG, "onCreate: ");

    

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) 
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState: ");
    

    @Override
    protected void onStart() 
        super.onStart();
        Log.i(TAG, "onStart: ");
    
	@Override
    protected void onSaveInstanceState(Bundle outState) 
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState: ");
    

    @Override
    protected void onResume() 
        super.onResume();
        Log.i(TAG, "onResume: ");
    

    @Override
    protected void onStop() 
        super.onStop();
        Log.i(TAG, "onStop: ");
    

    @Override
    protected void onPause() 
        super.onPause();
        Log.i(TAG, "onPause: ");
    

    @Override
    protected void onDestroy() 
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) 
        super.onConfigurationChanged(newConfig);
        Log.i(TAG, "onConfigurationChanged: ");
    


一、不修改androidManifest.xml

1、第一次创建

I/ScreenActivity: onCreate: 
I/ScreenActivity: onStart: 
I/ScreenActivity: onResume: 

2、切换横屏

I/ScreenActivity: onPause: 
I/ScreenActivity: onStop: 
I/ScreenActivity: onSaveInstanceState: 
I/ScreenActivity: onDestroy: 
I/ScreenActivity: onCreate: 
I/ScreenActivity: onStart: 
I/ScreenActivity: onRestoreInstanceState: 
    onResume: 

3、切换竖屏

I/ScreenActivity: onPause: 
I/ScreenActivity: onStop: 
I/ScreenActivity: onSaveInstanceState: 
I/ScreenActivity: onDestroy: 
I/ScreenActivity: onCreate: 
I/ScreenActivity: onStart: 
    onRestoreInstanceState: 
    onResume: 

二、修改AndroidManifest.xml

  1. orientation 屏幕在纵向和横向间旋转
  2. keyboardHidden 键盘显示或隐藏
  3. screenSize 屏幕大小改变了

1、android:configChanges=“orientation”

1.1、第一次创建

I/ScreenActivity: onCreate: 
I/ScreenActivity: onStart: 
I/ScreenActivity: onResume: 

1.2、切换横屏

I/ScreenActivity: onPause: 
I/ScreenActivity: onStop: 
I/ScreenActivity: onSaveInstanceState: 
I/ScreenActivity: onDestroy: 
I/ScreenActivity: onCreate: 
I/ScreenActivity: onStart: 
I/ScreenActivity: onRestoreInstanceState: 
    onResume: 

1.3、切换竖屏

I/ScreenActivity: onPause: 
I/ScreenActivity: onStop: 
I/ScreenActivity: onSaveInstanceState: 
I/ScreenActivity: onDestroy: 
I/ScreenActivity: onCreate: 
I/ScreenActivity: onStart: 
    onRestoreInstanceState: 
    onResume: 

2、android:configChanges=“orientation|screenSize”

2.1、第一次创建

I/ScreenActivity: onCreate: 
I/ScreenActivity: onStart: 
I/ScreenActivity: onResume: 

2.2、切换横屏

I/ScreenActivity: onConfigurationChanged: 

2.3、切换竖屏

I/ScreenActivity: onConfigurationChanged: 

3、android:configChanges=“orientation|screenSize|keyboardHidden”

3.1、第一次创建

I/ScreenActivity: onCreate: 
I/ScreenActivity: onStart: 
I/ScreenActivity: onResume: 

3.2、切换横屏

I/ScreenActivity: onConfigurationChanged: 

3.3、切换竖屏

I/ScreenActivity: onConfigurationChanged: 

以上是关于Activity生命周期-切换横竖屏-Android的主要内容,如果未能解决你的问题,请参考以下文章

Activity生命周期-切换横竖屏-Android

面试题:Activity横竖屏切换时的生命周期如何变化

Activity 在横竖屏切换情况下的生命周期变化

横竖屏切换时activity生命周期变化的总结

横竖屏切换时activity生命周期变化的总结

横竖屏切换时候activity的生命周期