您的位置:首页 > 移动开发 > Android开发

android activity横竖屏切换,Activity重新创建问题解决!

2013-10-21 12:26 477 查看
activityA和B A跳转到B

1:在主配置文件中添加Bactivity,如果不想B在横竖屏切换的时候导致activity的销毁和重新创建;可在声明activity中添加

android:configChanges="keyboardHidden|orientation"

[html]
view plaincopy

<!-- android:configChanges="keyboardHidden|orientation" 禁止activity在横竖屏的时候activity被重新创建或者重启,导致数据丢失,在游戏制作中尤为重要 -->
<activity
android:name=".DemoActivity"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.Dialog" />
<!-- android:theme="@android:style/Theme.Dialog" 设置该activity的显示样式是一对话框的形式显示 -->

2:在A的配置文件中添加一个按钮.和单击事件

3:重写A中父类的onCreate onDestroy onPause onRestart onResume onStart onStop 方法,并添加system.out的TAG

为了避免横竖屏和以为一些异常导致activity的销毁和数据丢失,就重写onSaveInstanceState这个方法,该方法可以保存当前页面因为异常导致的activity销毁时候数据,然后在下载onCreate的时候调用者写数据就可以了,onCreate中的参数就是onSaveInstanceState方法中放入的bundle对象

[java]
view plaincopy

package com.example.lifecycle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

/**
* 声明周期: 一个对象被创建出来 到 被销毁(或者垃圾回收)的这个过程中所必须的方法
*
* @author w
*
*/
public class MainActivity extends Activity {
/**
* A 激活下一个activity之前的页面 B激活下一个activity之后的页面<br>
* 程序第一次被部署的时候,执行第一个activity的onCreate onStart onResume 方法<br>
* 激活下一个activity 执行A的onPause B onCreate onStart onResume A的 onStop<br>
* 在B中点击返回按钮执行B的finidsh onPause A的 onRestart onStart onResume B的 onStop
* onDestroy<br>
* 在A中点击返回 执行 A的 finish onPause onStop onDestroy <br>
* 在A中点击home键 执行A的onPause onStop方法<br>
*/
@Override
/**
* activity第一次被创建的时候执行,可做一些初始化工作
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("main onCreate");
int page = 0;
if (savedInstanceState != null) {// savedInstanceState
// 接收因异常回收页面传来的bundle值
page = savedInstanceState.getInt("page");// 取得传来的bundle中的值
}
System.out.println("当前: " + page);
}

/**
* click点击事件
*/
public void click(View view) {
Intent intent = new Intent(this, DemoActivity.class);
startActivity(intent);
}

@Override
public void finish() {
super.finish();
System.out.println("main finish");
}

@Override
/**
* activity被销毁的时候执行的方法
*/
protected void onDestroy() {
super.onDestroy();
System.out.println("main onDestroy");
}

@Override
/**
* 当界面失去焦点的时候
*/
protected void onPause() {
super.onPause();
System.out.println("main onPause");
}

@Override
protected void onRestart() {
super.onRestart();
System.out.println("main onRestart");
}

@Override
/**
* 界面的控件可以获取焦点的时候
*/
protected void onResume() {
super.onResume();
System.out.println("main onResume");
}

@Override
/**
* 当界面可以被用户可见,当用户再次返回该activity的时候,可以用这个方法来做一些数据的更新
*/
protected void onStart() {
super.onStart();
System.out.println("main onStart");
}

@Override
/**
* 界面用户完全不可见的时候调用,比如正在看视屏,来一电话,可以用该方法停止播放
*/
protected void onStop() {
super.onStop();
System.out.println("main onStop");
}

@Override
/**
* 当activity被异常销毁(回收)的时候调用,如果是自适应屏幕,
* 如果是一个阅读器,在看的时候突然没电了,就会调用这个方法,当下次调用者个页面的时候,会调用onCreate方法,并把这个参数的bundle对象传给oncreate方法,<br>
* oncreate方法中的bundle参数就是该值
*/
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putInt("page", 30);
}

}

4:Bactivity代码

[java]
view plaincopy

package com.example.lifecycle;

import android.app.Activity;
import android.os.Bundle;

public class DemoActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
System.out.println("demo onCreate");
}

@Override
public void finish() {
super.finish();
System.out.println("demo finish");
}

@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("demo onDestroy");
}

@Override
protected void onPause() {
super.onPause();
System.out.println("demo onPause");
}

@Override
protected void onRestart() {
super.onRestart();
System.out.println("demo onRestart");
}

@Override
protected void onResume() {
super.onResume();
System.out.println("demo onResume");
}

@Override
protected void onStart() {
super.onStart();
System.out.println("demo onStart");
}

@Override
protected void onStop() {
super.onStop();
System.out.println("demo onStop");
}
}

注意onSaveInstanceState方法的作用,可以保存activity在因为异常导致销毁时候的数据,然后传给onCreate方法,在做处理,就可以避免数据的错乱,

例如 在看小说的时候切换了横竖屏,在切换的时候会调用onCreate onStart onResume 那么activity中的当前页数就丢了,用得从新开始,如果重写了onSaveInstanceState方法,把当前页放到参数bundle中,在onCreate的时候,onCreate中的参数bundle就是onSaveInstanceState方法中的bundle对象,然后判断onCreate参数bundle是否有值,如果有就取出对应的key值,如果是取出在切换前的当前页数,在判断不为空的时候
可以把页数直接定位到该页,这样就可以接着看了

在声明的activity中添加 android:configChanges="keyboardHidden|orientation"
属性 可以禁止activity在横竖屏的时候activity被重新创建或者重启,导致数据丢失,在游戏制作中尤为重要

通过设置这个属性可以使Activity捕捉设备状态变化,以下是可以被识别的内容:

CONFIG_FONT_SCALE

CONFIG_MCC

CONFIG_MNC

CONFIG_LOCALE

CONFIG_TOUCHSCREEN

CONFIG_KEYBOARD

CONFIG_NAVIGATION

CONFIG_ORIENTATION

设置方法:将下列字段用“|”符号分隔开,例如:“locale|navigation|orientation



ValueDescription
“mcc“The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the MCC.移动国家号码,由三位数字组成,每个国家都有自己独立的MCC,可以识别手机用户所属国家。
“mnc“The IMSI mobile network code (MNC) has changed — that is, a SIM hasbeen detected and updated the MNC.移动网号,在一个国家或者地区中,用于区分手机用户的服务商。
“locale“The locale has changed — for example, the user has selected a new language that text should be displayed in.用户所在地区发生变化。
“touchscreen“The touchscreen has changed. (This should never normally happen.)
“keyboard“The keyboard type has changed — for example, the user has plugged in an external keyboard.键盘模式发生变化,例如:用户接入外部键盘输入。
“keyboardHidden“The keyboard accessibility has changed — for example, the user has slid the keyboard out to expose it.用户打开手机硬件键盘
“navigation“The navigation type has changed. (This should never normally happen.)
“orientation“The screen orientation has changed — that is, the user has rotated the device.设备旋转,横向显示和竖向显示模式切换。
“fontScale“The font scaling factor has changed — that is, the user has selected a new global font size.全局字体大小缩放发生改变
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐