Android橫豎屏幕切換生命周期詳解
一、簡(jiǎn)介

二、代碼

/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)建一個(gè)button對(duì)象
private Button btn_stop;
private Button btn_offLine;
private String tag=MainActivity.class.getSimpleName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//父類(lèi)操作
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)建時(shí)執(zhí)行
*/
Log.d(tag, "onCreate");
}
/*
* activity可見(jiàn)時(shí)執(zhí)行
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(tag, "onStart");
}
/*
* activity交互時(shí)執(zhí)行
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(tag, "onResume");
}
/*
* activity重新可見(jiàn)時(shí)執(zhí)行
*/
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d(tag, "onRestart");
}
/*
* activity暫停時(shí)執(zhí)行
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(tag, "onPause");
}
/*
* activity停止時(shí)執(zhí)行
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(tag, "onStop");
}
/*
* activity銷(xiāo)毀時(shí)執(zhí)行
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(tag, "onDestroy");
}
/*
* activity在配置改變時(shí)執(zhí)行
* 比如橫豎屏幕的切換,鍵盤(pán)有無(wú)的切換,屏幕大小的改變
*/
@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://斷開(kāi)狀態(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)建一個(gè)button對(duì)象
private Button btn_stop;
private Button btn_offLine;
private String tag=MainActivity.class.getSimpleName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//父類(lèi)操作
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)建時(shí)執(zhí)行
*/
Log.d(tag, "onCreate");
}
/*
* activity可見(jiàn)時(shí)執(zhí)行
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(tag, "onStart");
}
/*
* activity交互時(shí)執(zhí)行
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(tag, "onResume");
}
/*
* activity重新可見(jiàn)時(shí)執(zhí)行
*/
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d(tag, "onRestart");
}
/*
* activity暫停時(shí)執(zhí)行
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(tag, "onPause");
}
/*
* activity停止時(shí)執(zhí)行
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(tag, "onStop");
}
/*
* activity銷(xiāo)毀時(shí)執(zhí)行
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(tag, "onDestroy");
}
/*
* activity在配置改變時(shí)執(zhí)行
* 比如橫豎屏幕的切換,鍵盤(pán)有無(wú)的切換,屏幕大小的改變
*/
@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://斷開(kāi)狀態(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android重力傳感器實(shí)現(xiàn)滾動(dòng)的彈球
- Android 重力傳感器在游戲開(kāi)發(fā)中的應(yīng)用
- Android開(kāi)發(fā)中的重力傳感器用法實(shí)例詳解
- 解析Android橫豎屏切換的問(wèn)題
- Android橫豎屏幕切換小結(jié)
- Android實(shí)現(xiàn)橫豎屏切換的實(shí)例代碼
- Android橫豎屏切換實(shí)例總結(jié)
- 解決Android手機(jī)屏幕橫豎屏切換
- Android 實(shí)現(xiàn)視頻字幕Subtitle和橫豎屏切換示例
- Android編程基于重力傳感器實(shí)現(xiàn)橫豎屏放向切換功能
相關(guān)文章
android 獲取APP的唯一標(biāo)識(shí)applicationId的實(shí)例
下面小編就為大家分享一篇android 獲取APP的唯一標(biāo)識(shí)applicationId的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Android使用SQLite數(shù)據(jù)庫(kù)的示例
本篇文章主要介紹了Android使用SQLite數(shù)據(jù)庫(kù)的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
Android Studio設(shè)置顏色拾色器工具Color Picker教程
這篇文章主要介紹了Android Studio設(shè)置顏色拾色器工具Color Picker教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
詳解Android使用Handler造成內(nèi)存泄露的分析及解決方法
這篇文章主要介紹了詳解Android使用Handler造成內(nèi)存泄露的分析及解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個(gè)方向準(zhǔn)確監(jiān)聽(tīng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個(gè)方向準(zhǔn)確監(jiān)聽(tīng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Android編程判斷手機(jī)上是否安裝了某個(gè)程序的方法
這篇文章主要介紹了Android編程判斷手機(jī)上是否安裝了某個(gè)程序的方法,涉及Android針對(duì)程序包的操作及進(jìn)程判斷的相關(guān)技巧,需要的朋友可以參考下2015-11-11
Android編程中出現(xiàn)The connection to adb is down問(wèn)題的解決方法
這篇文章主要介紹了Android編程中出現(xiàn)The connection to adb is down問(wèn)題的解決方法,涉及Android進(jìn)程與服務(wù)的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12

