Warning: mkdir(): No such file or directory in /www/wwwroot/news.ynwlzc.cn/index.php on line 31

Warning: mkdir(): No such file or directory in /www/wwwroot/news.ynwlzc.cn/index.php on line 35

Warning: file_put_contents(/www/wwwroot/news.ynwlzc.cn/log_userwlzc/2026/09/15/216.73.216.163_visit.csv): failed to open stream: No such file or directory in /www/wwwroot/news.ynwlzc.cn/index.php on line 54

Warning: file_put_contents(/www/wwwroot/news.ynwlzc.cn/log_userwlzc/2026/09/15/216.73.216.1632026_09_15_loginvisit.csv): failed to open stream: No such file or directory in /www/wwwroot/news.ynwlzc.cn/index.php on line 90

我们在mainActivity中写了android生命周期中几个重要的阶段onCreate,onResume,onStart,onDestory,onStop的方法


在其中都调用toast方法告诉我们这些阶段有没有触发。


另外我们重写onSaveInstanceState() 和 onRestoreInstanceState()


在他们里面也一样加上toast提示


这时候我们进入app,旋转手机我们会发现依次出现了onStop,onSaveInstanceState,onDestroy,onCreate,onStart,onRestoreInstanceState,onResume


我们就可以明白,默认情况下改变方向app将会循环一次生命周期,在测试中这样会比较容易导致app崩溃


为了避免这个情况,我们可以在manifest的activity中加入


android:configChanges="orientation|screenSize|keyboardHidden"

1

并且我们在mainActivity中加入


public void onConfigurationChanged(Configuration newConfig) {  

   // TODO Auto-generated method stub  

   super.onConfigurationChanged(newConfig);  

   if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {  

       Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();  

   } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {  

       Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();  

   }  

}  

1

2

3

4

5

6

7

8

9

这时候我们再改变手机方向,我们就会发祥,app并没有循环生命周期,而是触发了onConfigurationChanged的方法。

至于keyboardHidden的作用则是在旋转手机方向后隐藏keyboard。

————————————————

版权声明:本文为CSDN博主「zq_Hero」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/u013233097/article/details/78251351