Activity1.3

管理Activity的生命周期

你可以通过调用finish() 来终止activity

An activity can exist in essentially three states:

Resumed:The activity is in the foreground of the screen and has user focus(在屏幕的前面或者获取用户焦点)

Paused:Another activity is in the foreground and has focus, but this one is still visible,the Activity object is retained in memory, it maintains all state and member information, but can be killed by the system in extremely low memory situations.

Stopped:The activity is completely obscured(遮盖) by another activity,the activity is now in the "background",A stopped activity is also still alive ,it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere

If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process(调用finish或者直接kill)

onCreate():Called when the activity is first created.create views, bind data to lists, and so on,This method is passed a Bundle object containing the activity's previous state, if that state was captured ,Always followed by onStart().

onStop()Called when the activity is no longer visible to the user,This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.

onRestart()Called after the activity has been stopped, just prior to it being started again.

onStart()Called just before the activity becomes visible to the user.

onResume()Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it.

onPause()Called when the system is about to start resuming another activity,This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on,It should do whatever it does very quickly, because the next activity will not be resumed until it returns

onDestroy()This is the final call that the activity will receive.It could be called either because the activity is finishing , or because the system is temporarily destroying this instance of the activity to save space

The activity lifecycle.

package com.example.android_lifecycle;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

	private final String TAG = "MainActivity";
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "==onCreate");
    }
    
    @Override
    protected void onStart() {
    	super.onStart();
    	Log.i(TAG, "==onStart");
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	Log.i(TAG, "==onResume");
    }
    
    @Override
    protected void onRestart() {
    	super.onRestart();
    	Log.i(TAG, "==onRestart");
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    	Log.i(TAG, "==onPause");
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	Log.i(TAG, "==onStop");
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	Log.i(TAG, "==onDestroy");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
1、首次进入:
08-26 22:09:25.765: I/MainActivity(11029): ==onCreate
08-26 22:09:25.765: I/MainActivity(11029): ==onStart
08-26 22:09:25.765: I/MainActivity(11029): ==onResume

2、返回home:
08-26 22:09:37.480: I/MainActivity(11029): ==onPause
08-26 22:09:37.890: I/MainActivity(11029): ==onStop

3、再次进入:
08-26 22:09:53.910: I/MainActivity(11029): ==onRestart
08-26 22:09:53.910: I/MainActivity(11029): ==onStart
08-26 22:09:53.910: I/MainActivity(11029): ==onResume

4、返回键:
08-26 22:10:08.770: I/MainActivity(11029): ==onPause
08-26 22:10:09.145: I/MainActivity(11029): ==onStop
08-26 22:10:09.145: I/MainActivity(11029): ==onDestroy

Saving activity state

implementing an additional callback method that allows you to save information about the state of your activity: onSaveInstanceState().

Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient(临时的) state of the activity。you should never use it to store persistent data。

Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.(建议持久化)

猜你喜欢

转载自luan.iteye.com/blog/2109334
1.3