【问题标题】:landscape mode savedInstanceState横向模式 savedInstanceState
【发布时间】:2015-04-11 22:33:11
【问题描述】:

我正在开发一个 Android 应用程序并处理一个问题!

当我将屏幕从纵向模式更改为横向模式而不更改我正在处理的 android 活动时,停止、销毁,然后创建并启动我的活动。

为了知道这一点,我打印了 OnCreate()、onStart()、onRestart() ... 被调用的时间。

我想做的是在我只是改变屏幕模式时停止计数,或者当我改变屏幕模式时不要杀死和破坏我的活动。

这是我的代码

public class ActivityX extends Activity {

  
	private static final String RESTART_KEY = "restart";
	private static final String RESUME_KEY = "resume";
	private static final String START_KEY = "start";
	private static final String CREATE_KEY = "create";

	// String for LogCat documentation
	private final static String TAG = "ActivityX";

	// Lifecycle counters
	
	private int mCreate 	= 0;
	private int mRestart 	= 0;
	private int mStart 		= 0;
	private int mResume		= 0;

    //Create variables for each of the TextViews
	private static int mTvCreate	= 0;
	private static int mTvRestart 	= 0;
	private static int mTvStart		= 0;
	private static int mTvResume	= 0;


	
	TextView startTxtView ;
	TextView restartTxtView;
	TextView resumeTxtView;
	TextView createTxtView ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_one);
		
		
		startTxtView = (TextView) findViewById(R.id.start);
		restartTxtView = (TextView) findViewById(R.id.restart);
		resumeTxtView = (TextView) findViewById(R.id.resume);
		createTxtView = (TextView) findViewById(R.id.create);
		
		
		Button launchActivityYButton = (Button) findViewById(R.id.bLaunchActivityY);
		launchActivityYButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
	         try{
	        	 Intent intentActivity = new Intent(ActivityX.this, ActivityY.class);

	        	 startActivity(intentActivity);
	        	 

	        	 
	         } catch (Exception e) {
                 // Log any error messages to LogCat using Log.e()
                 Log.e(TAG, "On click X not working");
             }
	         


			}
		});

		// Has previous state been saved?
		if (savedInstanceState != null ) {


			mCreate 	= savedInstanceState.getInt(CREATE_KEY);
			mStart 		= savedInstanceState.getInt(START_KEY);
			mRestart 	= savedInstanceState.getInt(RESTART_KEY);
			mResume 	= savedInstanceState.getInt(RESUME_KEY);

		}

		// Emit LogCat message
		Log.i(TAG, "Entered the onCreate() method");


		
		mTvCreate 	=  mCreate + 1;
		mTvStart 	=  mStart;
		mTvRestart 	=  mRestart;
		mTvResume 	=  mResume;
		
		createTxtView.setText("onCreate()calls: "+mTvCreate);
		startTxtView.setText("onStart()calls: "+mTvStart);
		resumeTxtView.setText("onResume()calls: "+mTvResume);
		restartTxtView.setText("onRestart()calls: "+mTvRestart);

	}



	@Override
	public void onStart() {
		super.onStart();

		// Emit LogCat message
		Log.i(TAG, "Entered the onStart() method");

		mTvStart = mTvStart + 1;
		this.displayCounts();
	}

	@Override
	public void onResume() {
		super.onResume();

		// Emit LogCat message
		Log.i(TAG, "Entered the onResume() method");
		
		mTvResume = mTvResume + 1;
		this.displayCounts();
	}

	@Override
	public void onPause() {
		super.onPause();

		// Emit LogCat message
		Log.i(TAG, "Entered the onPause() method");
	}

	@Override
	public void onStop() {
		super.onStop();

		// Emit LogCat message
		Log.i(TAG, "Entered the onStop() method");
	}

	@Override
	public void onRestart() {
		super.onRestart();

		// Emit LogCat message
		Log.i(TAG, "Entered the onRestart() method");

		mTvRestart = mTvRestart + 1;
		this.displayCounts();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		// Emit LogCat message
		Log.i(TAG, "Entered the onDestroy() method");
	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {

		savedInstanceState.putInt(CREATE_KEY, mTvCreate);
	    savedInstanceState.putInt(RESTART_KEY, mTvRestart);
	    savedInstanceState.putInt(START_KEY, mTvStart);
	    savedInstanceState.putInt(RESUME_KEY, mTvResume);
	    
	    super.onSaveInstanceState(savedInstanceState);

	}


	public void displayCounts() {
	
		
		createTxtView.setText("onCreate() calls: " + mTvCreate);
		startTxtView.setText("onStart() calls: " + mTvStart);
		resumeTxtView.setText("onResume() calls: " + mTvResume);
		restartTxtView.setText("onRestart() calls: " + mTvRestart);
	
	}
}

【问题讨论】:

标签: android landscape savestate


【解决方案1】:

我没有足够的声誉来发表评论,但如果我理解正确,如果您满足以下条件,您可以忽略屏幕方向:

  1. onResume() 方法中开始计数。
  2. onPause() 方法中停止计数。

这样,您不必担心屏幕方向,因为只要方向开始,即设备物理旋转,就会调用onPause。而 onResume() 在定向完成时被调用。

【讨论】:

    【解决方案2】:

    现在一个快速的解决方法是将此行添加到您可以看到您的活动的 AndroidManifest.xml 中

    android:configChanges="orientation|screenSize"
    

    并在您的 Activity 中覆盖以下方法

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
    }
    

    现在您的活动不会被破坏

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多