Android橫豎屏幕切換生命周期詳解
一、簡介

二、代碼

/activityLifeCycle_3Screen/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fry.activityLifeCycle_3Screen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.fry.activityLifeCycle_3Screen.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity>
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fry.activityLifeCycle_3Screen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.fry.activityLifeCycle_3Screen.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity>
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity>
</application>
</manifest>
核心代碼:android:configChanges="keyboardHidden|orientation|screenSize"
com.fry.activityLifeCycle_3Screen.MainActivity
package com.fry.activityLifeCycle_3Screen;
import com.fry.activityLifeCycle_3Screen.R;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button btn_pause;//創(chuàng)建一個button對象
private Button btn_stop;
private Button btn_offLine;
private String tag=MainActivity.class.getSimpleName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//父類操作
setContentView(R.layout.activity_main);//引入名為activity_main的界面
btn_pause=(Button) findViewById(R.id.btn_pause);//找id為btn_pause的button
btn_stop=(Button) findViewById(R.id.btn_stop);//找id為btn_stop的button
btn_offLine=(Button) findViewById(R.id.btn_offLine);
btn_pause.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_offLine.setOnClickListener(this);
/*
* activity被創(chuàng)建時執(zhí)行
*/
Log.d(tag, "onCreate");
}
/*
* activity可見時執(zhí)行
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(tag, "onStart");
}
/*
* activity交互時執(zhí)行
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(tag, "onResume");
}
/*
* activity重新可見時執(zhí)行
*/
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d(tag, "onRestart");
}
/*
* activity暫停時執(zhí)行
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(tag, "onPause");
}
/*
* activity停止時執(zhí)行
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(tag, "onStop");
}
/*
* activity銷毀時執(zhí)行
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(tag, "onDestroy");
}
/*
* activity在配置改變時執(zhí)行
* 比如橫豎屏幕的切換,鍵盤有無的切換,屏幕大小的改變
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d(tag, "onConfigurationChanged");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_pause:
Intent intent=new Intent();
intent.setClass(this, Activity02.class);
startActivity(intent);
break;
case R.id.btn_stop:
Intent intent2=new Intent();
intent2.setClass(this, Activity01.class);
startActivity(intent2);
break;
case R.id.btn_offLine://斷開狀態(tài)
finish();
default:
break;
}
}
}
三、一直橫屏或者一直豎屏
很多手機(jī)游戲里面一進(jìn)去就是橫屏,而且不能切換為豎屏,那么怎么樣達(dá)到這樣的效果呢?
/activityLifeCycle_3Screen/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fry.activityLifeCycle_3Screen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:screenOrientation="portrait"
android:name="com.fry.activityLifeCycle_3Screen.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity>
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity>
</application>
</manifest>
android:screenOrientation="landscape"橫屏
android:screenOrientation="portrait"豎屏
四、如何獲取手機(jī)是橫屏還是豎屏

com.fry.activityLifeCycle_3Screen.MainActivity
package com.fry.activityLifeCycle_3Screen;
import com.fry.activityLifeCycle_3Screen.R;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button btn_pause;//創(chuàng)建一個button對象
private Button btn_stop;
private Button btn_offLine;
private String tag=MainActivity.class.getSimpleName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//父類操作
setContentView(R.layout.activity_main);//引入名為activity_main的界面
btn_pause=(Button) findViewById(R.id.btn_pause);//找id為btn_pause的button
btn_stop=(Button) findViewById(R.id.btn_stop);//找id為btn_stop的button
btn_offLine=(Button) findViewById(R.id.btn_offLine);
btn_pause.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_offLine.setOnClickListener(this);
/*
* activity被創(chuàng)建時執(zhí)行
*/
Log.d(tag, "onCreate");
}
/*
* activity可見時執(zhí)行
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(tag, "onStart");
}
/*
* activity交互時執(zhí)行
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(tag, "onResume");
}
/*
* activity重新可見時執(zhí)行
*/
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d(tag, "onRestart");
}
/*
* activity暫停時執(zhí)行
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(tag, "onPause");
}
/*
* activity停止時執(zhí)行
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(tag, "onStop");
}
/*
* activity銷毀時執(zhí)行
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(tag, "onDestroy");
}
/*
* activity在配置改變時執(zhí)行
* 比如橫豎屏幕的切換,鍵盤有無的切換,屏幕大小的改變
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d(tag, "onConfigurationChanged");
int width=getWindowManager().getDefaultDisplay().getWidth();
int height=getWindowManager().getDefaultDisplay().getHeight();
if(width>height) Log.d(tag, "landscape");
else Log.d(tag, "portrait");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_pause:
Intent intent=new Intent();
intent.setClass(this, Activity02.class);
startActivity(intent);
break;
case R.id.btn_stop:
Intent intent2=new Intent();
intent2.setClass(this, Activity01.class);
startActivity(intent2);
break;
case R.id.btn_offLine://斷開狀態(tài)
finish();
default:
break;
}
}
}
/activityLifeCycle_3Screen/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fry.activityLifeCycle_3Screen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.fry.activityLifeCycle_3Screen.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity>
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity>
</application>
</manifest>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android 獲取APP的唯一標(biāo)識applicationId的實(shí)例
下面小編就為大家分享一篇android 獲取APP的唯一標(biāo)識applicationId的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
Android使用SQLite數(shù)據(jù)庫的示例
本篇文章主要介紹了Android使用SQLite數(shù)據(jù)庫的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
Android Studio設(shè)置顏色拾色器工具Color Picker教程
這篇文章主要介紹了Android Studio設(shè)置顏色拾色器工具Color Picker教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
詳解Android使用Handler造成內(nèi)存泄露的分析及解決方法
這篇文章主要介紹了詳解Android使用Handler造成內(nèi)存泄露的分析及解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個方向準(zhǔn)確監(jiān)聽
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個方向準(zhǔn)確監(jiān)聽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
Android編程判斷手機(jī)上是否安裝了某個程序的方法
這篇文章主要介紹了Android編程判斷手機(jī)上是否安裝了某個程序的方法,涉及Android針對程序包的操作及進(jìn)程判斷的相關(guān)技巧,需要的朋友可以參考下2015-11-11
Android編程中出現(xiàn)The connection to adb is down問題的解決方法
這篇文章主要介紹了Android編程中出現(xiàn)The connection to adb is down問題的解決方法,涉及Android進(jìn)程與服務(wù)的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12

