管理Activity的生命周期


Only three ofthese states can be static. That is, the activity can exist in one of onlythree states for an extended period of time:

Resumed

In this state, the activity is in the foregroundand the user can interact with it. (Also sometimes referred to as the"running" state.)

Paused

In this state, the activity is partially obscuredby another activity—the other activity that's in the foreground issemi-transparent or doesn't cover the entire screen. The paused activity doesnot receive user input and cannot execute any code.

Stopped

In this state, the activity is completely hiddenand not visible to the user; it is considered to be in the background. Whilestopped, the activity instance and all its state information such as membervariables is retained, but it cannot execute any code.

 

The other states(Created and Started) are transient and the system quickly moves from them tothe next state by calling the next lifecycle callback method. That is, afterthe system calls onCreate(), it quickly calls onStart(), which is quickly followed by onResume().

 

Createand destroy the Activity

1.        Create the Activity

You mustimplement the onCreate() method to perform basic application startup logic that shouldhappen only once for the entire life of the activity. For example, yourimplementation of onCreate() should define the user interface and possiblyinstantiate some class-scope variables.

Once the onCreate() finishes execution, the system calls the onStart() and onResume() methods in quick succession. Your activity never resides in the Created or Started states. Technically, the activity becomes visible to the user when onStart() is called, but onResume() quickly follows and the activity remains in the Resumed state untilsomething occurs to change that, such as when a phone call is received, theuser navigates to another activity, or the device screen turns off.


Figure.Another illustration of the activity lifecycle structure with an emphasis onthe three main callbacks that the system calls in sequence when creating a newinstance of the activity: onCreate(), onStart(), and onResume(). Once this sequence of callbacks complete, theactivity reaches the Resumed state where users caninteract with the activity until they switch to a different activity.

2.        Destroy the Activity

Most apps don't need to implement this method because local class references are destroyed with the activity andyour activity should perform most cleanup during onPause() and onStop(). However, if your activity includesbackground threads that you created during onCreate() or other other long-running resources that couldpotentially leak memory if not properly closed, you should kill them during onDestroy().

Note: The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: when you call finish() from within the onCreate() method. In some cases,such as when your activity operates as a temporary decision maker to launchanother activity, you might call finish() from within onCreate() to destroy the activity. In this case, the system immediately callsonDestroy() without calling any of the other lifecycle methods.

 

Pause and Resume the Activity


Figure. When a semi-transparent activity obscures your activity, the systemcalls onPause() and the activity waits in the Paused state (1). If the user returnsto the activity while it's still paused, the system calls onResume() (2).

1.        Pause the Activity

When the system calls onPause()for your activity, it means your activity is still partially visible, but mostoften is an indication that the user is leaving the activity and it will soonenter the Stopped state. You should usually use the onPause()callback to:

1)        Stopanimations or other ongoing actions that could consume CPU.

2)        Commitunsaved changes, but only if users expect such changes to be permanently savedwhen they leave (such as a draft email).

3)        Releasesystem resources, such as broadcast receivers, handles to sensors (like GPS),or any resources that may affect battery life.

 

you should avoid performing CPU-intensive work during onPause(), such as writing to adatabase, because it can slow the visible transition to the next activity

2.        Resume the Activity

When the userresumes your activity from the Paused state, the system calls the onResume() method. You should implement onResume() to initialize components that you release during onPause().

 

Stopand Restart the Activity


1.        Stop the Activity

When your activity receives a call to the onStop() method, it's no longer visible and should releasealmost all resources that aren't needed while the useris not using it. Once your activity is stopped, the system might destroy theinstance if it needs to recover system memory. In extreme cases, the systemmight simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory( suchas writing information to a database).

2.        Restart the Activity

You shouldusually use the onStart() callback method as the counterpart to the onStop() method, because the system calls onStart() both when it creates your activity and when it restarts theactivity from the stopped state.

 

Recreatethe Activity

if the system destroysthe activity due to system constraints (rather thannormal app behavior), then althought the actual Activity instance is gone, the system remembers that it existed such that ifthe user navigates back to it, the system creates a new instance of theactivity using a set of saved data that describes the state of the activitywhen it was destroyed. The saved data that the system uses to restore theprevious state is called the "instance state" and is a collection ofkey-value pairs stored in a Bundle object.

 

Figure. As the system begins to stop your activity, it calls onSaveInstanceState() (1) so you can specify additional state data you'd like to save incase the Activity instance must be recreated. If the activity is destroyed and thesame instance must be recreated, the system passes the state data defined at(1) to both the onCreate() method (2) and the onRestoreInstanceState() method (3).

 

1.        Save the Activity State

 

2.        Restore the Activity State

When youractivity is recreated after it was previously destroyed, you can recover yoursaved state from the Bundle that the system passes your activity. Boththe onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that containes the instance state information.

Because the onCreate() method is called whether the system is creating anew instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then thesystem is creating a new instance of the activity, instead of restoring aprevious one that was destroyed.

Instead ofrestoring the state during onCreate() you may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need tocheck whether the Bundle is null.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值