android 横竖屏和布局问题
在这里我做了一个小test,之前看过关于这方面的东西,到后来我才真正的遇到项目的时候,不得已才要去真正的熟悉里面的流程。
这里我贴测试源码:
TestActivity.java
package cn.com.hrmdemo;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle arg0) {
System.out.println("------ TestActivity onCreate------");
Toast.makeText(this, "onCreate。。。", Toast.LENGTH_SHORT).show();
super.onCreate(arg0);
setContentView(R.layout.test_linearlayout);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
System.out.println("------ TestActivity onRestoreInstanceState------");
Toast.makeText(this, "onRestoreInstanceState。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onStart() {
System.out.println("------ TestActivity onStart------");
Toast.makeText(this, "onStart。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onRestart() {
System.out.println("------ TestActivity onRestart------");
Toast.makeText(this, "onRestart。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onRestart();
}
@Override
protected void onResume() {
System.out.println("------ TestActivity onResume------");
Toast.makeText(this, "onResume。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
System.out.println("------ TestActivity onSaveInstanceState------");
Toast.makeText(this, "onSaveInstanceState。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
@Override
protected void onPause() {
System.out.println("------ TestActivity onPause------");
Toast.makeText(this, "onPause。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onStop() {
System.out.println("------ TestActivity onStop------");
Toast.makeText(this, "onStop。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onStop();
}
@Override
protected void onDestroy() {
System.out.println("------ TestActivity onDestroy------");
Toast.makeText(this, "onDestroy。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
System.out.println("------ TestActivity onConfigurationChanged------");
Toast.makeText(this, "屏幕转变了。。。", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
}
------------------------------------------------------上面是源码----------------------------------------------------------
下面看看demo的配置和运行的效果是怎样
这个横竖屏的配置主要是在AndroidManiFest.xml,就是Android App 的清单文件:
<activity .....
android:configChanges="想要配置的方式"
>
在你的activity 里面配置你的横竖屏切换方式
1). 什么都不做配置的情况下
a.正常情况下:
07-24 08:43:59.519: I/System.out(18726): ------ TestActivity onCreate------
07-24 08:43:59.609: I/System.out(18726): ------ TestActivity onStart------
07-24 08:43:59.619: I/System.out(18726): ------ TestActivity onResume------
b.切换横屏情况下:
07-24 08:51:01.359: I/System.out(18936): ------ TestActivity onPause------
07-24 08:51:01.379: I/System.out(18936): ------ TestActivity onSaveInstanceState------
07-24 08:51:01.389: I/System.out(18936): ------ TestActivity onStop------
07-24 08:51:01.399: I/System.out(18936): ------ TestActivity onDestroy------
07-24 08:51:01.429: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5bb4d000 size:4608000 offset:3072000
07-24 08:51:01.429: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5c8c5000 size:16531456 offset:14995456
07-24 08:51:01.449: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:51:01.579: I/System.out(18936): ------ TestActivity onStart------
07-24 08:51:01.579: I/System.out(18936): ------ TestActivity onRestoreInstanceState------
07-24 08:51:01.599: I/System.out(18936): ------ TestActivity onResume------
c.再切换回竖屏的情况下:
07-24 08:51:42.669: I/System.out(18936): ------ TestActivity onPause------
07-24 08:51:42.689: I/System.out(18936): ------ TestActivity onSaveInstanceState------
07-24 08:51:42.699: I/System.out(18936): ------ TestActivity onStop------
07-24 08:51:42.729: I/System.out(18936): ------ TestActivity onDestroy------
07-24 08:51:42.759: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5c8c5000 size:4608000 offset:3072000
07-24 08:51:42.759: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5cd2a000 size:10797056 offset:9261056
07-24 08:51:42.789: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:51:42.859: I/System.out(18936): ------ TestActivity onStart------
07-24 08:51:42.859: I/System.out(18936): ------ TestActivity onRestoreInstanceState------
07-24 08:51:42.879: I/System.out(18936): ------ TestActivity onResume------
2)配置android:configChanges="orientation|keyboardHidden" (4.0以前)
a.正常情况下:
07-24 08:54:05.059: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:54:05.199: I/System.out(18936): ------ TestActivity onStart------
07-24 08:54:05.219: I/System.out(18936): ------ TestActivity onResume------
b.切换横屏情况下:
07-24 08:54:25.909: I/System.out(18936): ------ TestActivity onPause------
07-24 08:54:25.919: I/System.out(18936): ------ TestActivity onSaveInstanceState------
07-24 08:54:25.929: I/System.out(18936): ------ TestActivity onStop------
07-24 08:54:25.949: I/System.out(18936): ------ TestActivity onDestroy------
07-24 08:54:25.979: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5c7c5000 size:19603456 offset:18067456
07-24 08:54:25.979: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5da77000 size:4608000 offset:3072000
07-24 08:54:26.019: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:54:26.069: I/System.out(18936): ------ TestActivity onStart------
07-24 08:54:26.079: I/System.out(18936): ------ TestActivity onRestoreInstanceState------
07-24 08:54:26.079: I/System.out(18936): ------ TestActivity onResume------
c.再切换回竖屏的情况下:
07-24 08:55:02.099: I/System.out(18936): ------ TestActivity onPause------
07-24 08:55:02.109: I/System.out(18936): ------ TestActivity onSaveInstanceState------
07-24 08:55:02.119: I/System.out(18936): ------ TestActivity onStop------
07-24 08:55:02.129: I/System.out(18936): ------ TestActivity onDestroy------
07-24 08:55:02.139: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5c7c5000 size:4608000 offset:3072000
07-24 08:55:02.139: D/memalloc(18936): /dev/pmem: Unmapping buffer base:0x5cc30000 size:9216000 offset:7680000
07-24 08:55:02.179: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:55:02.249: I/System.out(18936): ------ TestActivity onStart------
07-24 08:55:02.289: I/System.out(18936): ------ TestActivity onRestoreInstanceState------
07-24 08:55:02.309: I/System.out(18936): ------ TestActivity onResume------
发觉上面的结果是跟网上的朋友说的不是一样的,原来是有区别的。
到后来去网上搜索了一下资料发觉,到了。4.0以后。要加上这样的配置才会使Activity的生命周期不会重新被调用
3)配置android:configChanges="keyboardHidden|orientation|screenSize" (4.0以后)
a.正常情况下:
07-24 08:54:05.059: I/System.out(18936): ------ TestActivity onCreate------
07-24 08:54:05.199: I/System.out(18936): ------ TestActivity onStart------
07-24 08:54:05.219: I/System.out(18936): ------ TestActivity onResume------
b.切换横屏情况下:
07-24 09:08:21.639: I/System.out(19533): ------ TestActivity onConfigurationChanged------
c.再切换回竖屏的情况下:
07-24 09:08:21.639: I/System.out(19533): ------ TestActivity onConfigurationChanged------
总结:要想Activity在横竖屏切换的时候不重新加载生命周期的重要方法,一定要加上配置
4.0之前
<activity .....
android:configChanges="orientation|keyboardHidden"
>
4.0之后
<activity .....
android:configChanges="keyboardHidden|orientation|screenSize"
>
------------------------------------------------------常用设置---------------------------------------------------------
1.如果你的app指定要横屏显示的话
<activity .....
android:screenOrientation="portrait"
>
2.如果你的app指定要竖屏显示的话
<activity .....
android:screenOrientation="landscape"
>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。