activity横屏竖屏的切换

原理:

activity横屏竖屏的切换_第1张图片

activity横屏竖屏的切换_第2张图片


其实总结起来,我们可以得到以下的一些结论

1、当内存不足(不容易模拟)、切屏时会调用onSaveInstanceState()、onRestoreInstanceState()方法

  对于onSaveInstanceState()方法

   1)用于保存临时性数据,在创建下一个Activity是调用

   2)在onPause()方法之前调用(这个很容易理解)

   3)保存的数据可以在onRestoreInstanceState()方法、onCreate()方法中取到

   4)数据的传递是这样子的。通过putInt(key,value)  存 [当然还可以用其他类型] ,

用过getInt(key)来取


2、横竖屏切换的时候:
竖屏到横屏:activity首先被销毁,在创建一个新的activity
横屏到竖屏:activity首先被销毁,再创建一个新的activity,再把该activity销毁,再创建一个新的activity.(是由于输入法的影响)


1)如果希望在切屏时只销毁1次activity和只创建一次activity,则可以在<activity>配上android:configChanges="orientation"


2)如果希望切屏时保留原来的哪一个activity不变,则可以这样配置

android:configChanges="orientation|keyboardHidden"



1、MainActivity

package com.njupt.activitylife;

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

public class MainActivity extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		System.out.println("onCreate");
		
		if(savedInstanceState != null){
			int page = savedInstanceState.getInt("page");
			System.out.println("onCreate() page: " +page);
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		
		System.out.println("onStart()");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	    
		System.out.println("onResume()");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	    
		System.out.println("onPause()");
	}
	
	
	@Override
	protected void onStop() {
		super.onStop();
	    
		System.out.println("onStop()");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	    
		System.out.println("onDestroy()");
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		
		System.out.println("onRestart()");
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		
		outState.putInt("page", 50);
		
		System.out.println("onSaveInstanceState()");
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	    
		int page = savedInstanceState.getInt("page");
		System.out.println("onRestoreInstanceState() page: " + page);
	}
	
	
	public void get(View v){
		
		Intent intent = new Intent(this,Main2Activity.class);
		
		startActivity(intent);
	}
	
	/**
	 * 对横屏和竖屏进行一些特定的处理
	 */
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	    
		if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
			System.out.println("现在是横屏");
		}else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
			System.out.println("现在是竖屏");
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


2、AndroidManifest.xml

<activity
            android:name="com.njupt.activitylife.MainActivity"
            android:label="@string/app_name" 
            android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        



你可能感兴趣的:(activity横屏竖屏的切换)