• Android横竖屏切换和灭屏亮屏时Activity的生命周期探究(1)


    研究这个问题的初衷在于项目中碰到了一个问题:横屏的时候灭屏再亮屏,亮屏的时候用户能够清晰的看到先启动竖屏(过程1)再切换到横屏的过程,因为灭屏的时候onSaveInstanceState()保存的时横屏时的状态信息,因此过程1竖屏会使用到横屏的状态參数并且这一过程用户是可见的。因此会导致一些意想不到的Bug的出现。

    探究使用的实例中我用了屏幕的横竖屏和宽这两个配置信息来说明生命周期中此时系统所知道的屏幕的客观状态,注意这个客观状态与我们在onSaveInstanceState()中要保存的“状态信息”是不一样的,客观状态由硬件和系统决定。在那一时刻一定是这种状态,“状态信息”是程序猿想要保存的信息,这个由程序猿自己控制。

    横竖屏切换通常会由onConfigurationChanged()这个系统函数来响应,但响应这个函数须要在Manifest中为Activity配置例如以下信息(Android3.2及更高版本号的配置方法)

    <activity
                ...
                android:configChanges="orientation|screenSize"
                ... >

    本篇先探究没有进行这种配置时Activity的生命状态,从结果中能够看到此时的横竖屏切换时一个全然杀死Activity再重新启动Activity的过程,同一时候横屏的灭屏亮屏都先做了一次切换到竖屏的过程。

    源代码例如以下:

    package com.vivo.configurationtest;
    
    import android.app.Activity;
    import android.content.res.Configuration;
    import android.os.Bundle;
    import android.util.Log;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		Log.d("vi", "---onCreate()--" + "orientation = " + orientation()
    				+ "width = " + width());
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    	}
    
    	@Override
    	protected void onSaveInstanceState(Bundle outState) {
    		// TODO Auto-generated method stub
    		Log.d("vi", "---onSaveInstanceState()--" + "orientation = "
    				+ orientation() + "width = " + width());
    		super.onSaveInstanceState(outState);
    	}
    
    	@Override
    	protected void onStart() {
    		// TODO Auto-generated method stub
    		Log.d("vi", "---onStart()--" + "orientation = " + orientation()
    				+ "width = " + width());
    		super.onStart();
    	}
    
    	@Override
    	protected void onResume() {
    		// TODO Auto-generated method stub
    		Log.d("vi", "---onResume()--" + "orientation = " + orientation()
    				+ "width = " + width());
    		super.onResume();
    	}
    
    	@Override
    	protected void onPause() {
    		// TODO Auto-generated method stub
    		Log.d("vi", "---onPause()--" + "orientation = " + orientation()
    				+ "width = " + width());
    		super.onPause();
    	}
    
    	@Override
    	public void onConfigurationChanged(Configuration newConfig) {
    		// TODO Auto-generated method stub
    		Log.d("vi", "---onConfigurationChanged()--" + "width = " + width());
    		super.onConfigurationChanged(newConfig);
    	}
    
    	@Override
    	protected void onStop() {
    		// TODO Auto-generated method stub
    		Log.d("vi", "---onStop()--" + "orientation = " + orientation()
    				+ "width = " + width());
    		super.onStop();
    	}
    
    	@Override
    	protected void onRestart() {
    		// TODO Auto-generated method stub
    		Log.d("vi", "---onRestart()--" + "orientation = " + orientation()
    				+ "width = " + width());
    		super.onRestart();
    	}
    
    	@Override
    	protected void onDestroy() {
    		// TODO Auto-generated method stub
    		Log.d("vi", "---onDestroy()--" + "orientation = " + orientation()
    				+ "width = " + width());
    		super.onDestroy();
    	}
    
    	private String orientation() {
    
    		int ori = this.getResources().getConfiguration().orientation;
    		if (ori == Configuration.ORIENTATION_PORTRAIT) {
    			return "ORIENTATION_PORTRAIT--";
    		} else if (ori == Configuration.ORIENTATION_LANDSCAPE) {
    			return "ORIENTATION_LANDSCAPE--";
    		}
    		return "-------";
    	}
    
    	private int width() {
    		int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    		return screenWidth;
    	}
    }
    


    //下面为没有在manifest中配置configuration

    //竖屏从创建到切换到横屏
    C:UsersAdministrator>adb logcat -s vi
    --------- beginning of /dev/log/main
    --------- beginning of /dev/log/system
    D/vi      (16610): ---onCreate()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onStart()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onResume()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onPause()--orientation = ORIENTATION_LANDSCAPE--width = 854  //onPause()时已经获取到切换之后的屏幕的配置信息
    D/vi      (16610): ---onSaveInstanceState()--orientation = ORIENTATION_LANDSCAPE--width = 854
    D/vi      (16610): ---onStop()--orientation = ORIENTATION_LANDSCAPE--width = 854
    D/vi      (16610): ---onDestroy()--orientation = ORIENTATION_LANDSCAPE--width = 854 //屏幕切换是一个全然的杀死activity然后重新启动activity的过程
    D/vi      (16610): ---onCreate()--orientation = ORIENTATION_LANDSCAPE--width =854
    D/vi      (16610): ---onStart()--orientation = ORIENTATION_LANDSCAPE--width = 854
    D/vi      (16610): ---onResume()--orientation = ORIENTATION_LANDSCAPE--width =854




    //从横屏切换到竖屏
    C:UsersAdministrator>adb logcat -s vi
    --------- beginning of /dev/log/main
    --------- beginning of /dev/log/system
    D/vi      (16610): ---onPause()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onSaveInstanceState()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onStop()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onDestroy()--orientation = ORIENTATION_PORTRAIT--width =480
    D/vi      (16610): ---onCreate()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onStart()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onResume()--orientation = ORIENTATION_PORTRAIT--width = 480


    //竖屏从灭屏到亮屏
    C:UsersAdministrator>adb logcat -s vi
    --------- beginning of /dev/log/main
    --------- beginning of /dev/log/system
    D/vi      (16610): ---onPause()--orientation = ORIENTATION_PORTRAIT--width = 480 //灭屏从onPause()处開始
    D/vi      (16610): ---onSaveInstanceState()--orientation = ORIENTATION_PORTRAIT--width = 480 //暂停并保存当前信息,要保存什么样的信息由用户选择
    D/vi      (16610): ---onStop()--orientation = ORIENTATION_PORTRAIT--width = 480  
    D/vi      (16610): ---onRestart()--orientation = ORIENTATION_PORTRAIT--width =480 //亮屏从onRestart()处開始
    D/vi      (16610): ---onStart()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onResume()--orientation = ORIENTATION_PORTRAIT--width = 480


    //横屏的灭屏
    C:UsersAdministrator>adb logcat -s vi
    --------- beginning of /dev/log/main
    --------- beginning of /dev/log/system
    D/vi      (16610): ---onPause()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onSaveInstanceState()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onStop()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onDestroy()--orientation = ORIENTATION_PORTRAIT--width =480
    D/vi      (16610): ---onCreate()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onStart()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onResume()--orientation = ORIENTATION_PORTRAIT--width = 480 //横屏的灭屏相当于先切换到竖屏,再灭屏。这个过程用户没看到


    D/vi      (16610): ---onPause()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onSaveInstanceState()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onStop()--orientation = ORIENTATION_PORTRAIT--width = 480


    //横屏的亮屏
    C:UsersAdministrator>adb logcat -s vi
    --------- beginning of /dev/log/main
    --------- beginning of /dev/log/system
    D/vi      (16610): ---onRestart()--orientation = ORIENTATION_PORTRAIT--width =480
    D/vi      (16610): ---onStart()--orientation = ORIENTATION_PORTRAIT--width = 480
    D/vi      (16610): ---onResume()--orientation = ORIENTATION_PORTRAIT--width = 480 //横屏的亮屏相当于先亮到竖屏,再切换到横屏。这个过程用户是能够看到的,灭屏的时候系统也做了一次相同的切换到竖屏的动作,屏灭用户没看到而已


    D/vi      (16610): ---onPause()--orientation = ORIENTATION_LANDSCAPE--width = 854
    D/vi      (16610): ---onSaveInstanceState()--orientation = ORIENTATION_LANDSCAPE--width = 854
    D/vi      (16610): ---onStop()--orientation = ORIENTATION_LANDSCAPE--width = 854
    D/vi      (16610): ---onDestroy()--orientation = ORIENTATION_LANDSCAPE--width = 854
    D/vi      (16610): ---onCreate()--orientation = ORIENTATION_LANDSCAPE--width =854
    D/vi      (16610): ---onStart()--orientation = ORIENTATION_LANDSCAPE--width = 854
    D/vi      (16610): ---onResume()--orientation = ORIENTATION_LANDSCAPE--width =854

  • 相关阅读:
    MySQL集群搭建(4)-MMM+LVS+Keepalived
    MySQL集群搭建(3)-MMM高可用架构
    MySQL集群搭建(2)-主主从模式
    MySQL集群搭建(1)-主备搭建
    MySQL 安装(二进制版)
    Nginx缓存了DNS解析造成后端不通--代理
    开启tcp_timestamps和tcp_tw_recycle造成NAT转发连接不上
    tcp_tw_recycle参数引发的故障
    记一次TIME_WAIT网络故障
    TCP服务端收到syn但是不回复syn ack问题分析
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10530916.html
  • Copyright © 2020-2023  润新知