Start activity last lesson the next textbook course will show you how to
- Understanding Life cycle Callbacks
- Specify the launcher activity for your app
- Create a new instance
- Destroying activity
You should also read
Try a demo download
Activitylifecycle.zip
Unlike main()
other programming paradigms that use methods to launch applications, the Android system launches code in an instance by invoking a specific callback method corresponding to a specific stage in its life cycle Activity
. There is a series of callback methods that initiate activity, and a series of callback methods that decompose the activity.
This course outlines the most important life-cycle approaches and shows you how to handle the first lifecycle callback that creates a new instance of activity.
Understanding Life cycle Callbacks
During the activity's life cycle, the system invokes a set of core life cycle methods in a sequence similar to a ladder pyramid. In other words, each stage of the activity's life cycle is the first order of the pyramid. When the system creates a new activity instance, each callback method moves the activity state to the top of the first order. The top of the pyramid is the point in time that the activity runs in the foreground and the user can interact with it.
When the user starts to leave the activity, the system calls other methods to move the activity state down in the pyramid, destroying the activity. In some cases, activity will move down and wait only partially in the pyramid (for example, when the user switches to another app), the activity can move from that point back to the top (if the user returns to the activity) and continue where the user stopped.
Figure 1. Simplified activity life cycle diagram, expressed as a ladder pyramid. This illustration shows that for each callback that is used to move the activity to the top of the "continue" state, there is a callback method that moves the activity down one step. The activity can also return to the resume state from the pause and stop states.
Depending on the complexity of your activity, you may not need to implement all life cycle methods. However, it is important to understand each method and implement a method that ensures that your app runs in the way the user expects it to. Correctly implementing your Activity lifecycle approach ensures that your app works well in several ways, including:
- If a user listens to an incoming call or switches to another app while using your app, it doesn't crash.
- Valuable system resources are not consumed when the user is not actively using it.
- If a user leaves your app and returns later, the user's progress will not be lost.
- When the screen rotates between landscape and portrait, it does not crash or lose the user's progress.
As you will learn in the following courses, there are several situations where activity transitions between different states as shown in Figure 1. However, only three of these states can be static. In other words, activity can only exist for a long time in three states.
-
Go on
-
In this state, the activity is in the foreground and the user can interact with it. (sometimes also called a "run" state.) )
-
Time out
-
In this state, the activity is blocked in the foreground in a semitransparent state or another activity-that does not cover the entire screen. Paused activity does not receive user input and cannot execute any code.
-
Stop it
-
in this state, the activity is completely hidden and invisible to the user, and it is considered to be in the background. When stopped, the activity instance and all state information, such as member variables, are preserved, but it cannot execute any code.
Other states (Create and start) are transient, and the system quickly moves from these states to the next state by invoking the next life cycle callback method. That is, after the system call onCreate()
, it is called quickly onStart()
, followed by a quick call onResume()
.
The Basic life cycle section ends here. You will now begin to learn some knowledge of specific life cycle behavior.
Specify the launcher activity for your app
When a user selects your app icon from the Home screen, the system invokes the method in an app that you have declared as the initiator (or "primary") activity Activity
onCreate()
. This is activity as the main portal for your app's user interface.
You can AndroidManifest.xml
define which activity is used as the primary activity in the Android declarative file, which is located in the root directory of your project directory.
The main activity of your app must be declared in the declarative statement using <intent-filter>
(including MAIN
actions and LAUNCHER
categories). For example:
<activity Android:name=". 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>
Note: When you use the Android SDK tool to create a new Android project, the default project file includes classes that are declared in the declarative description using the filter Activity
.
If you do not declare an action or category for one of your activity MAIN
LAUNCHER
, your app icon will not appear in the app's home screen list.
Create a new instance
Most apps contain several different activity that users can perform different actions on. Whether the activity is the main activity created when the user clicks on your app icon or other activity that your app started in response to a user action, the system onCreate()
creates each new instance by calling its method Activity
.
You must implement onCreate()
the basic application startup logic that the method execution should only appear once during the activity's entire life cycle. For example, your onCreate()
implementation should define a user interface and possibly instantiate some class-wide variables.
For example, onCreate()
The following example of a method shows some code that executes some of the basic settings of the activity, such as declaring a user interface (defined in an XML layout file), defining member variables, and configuring some UI.
TextViewMtextview; Member variable for text view in the layout
@Override
Public voidOnCreate(BundleSavedinstancestate) {
Super.OnCreate(Savedinstancestate);
Set the user interface layout for this Activity
The layout file is defined in the project Res/layout/main_activity.xml file
Setcontentview(R.Layout.Main_activity);
Initialize member TextView So we can manipulate it later
Mtextview= (TextView)Findviewbyid(R.Id.Text_message);
Make sure we ' re running on honeycomb or higher to use ActionBar APIs
If (Build.VERSION.Sdk_int>= Build.version_codes. {
//for the main activity, make sure the app icon in the action Bar
&N Bsp //does not behave as a button
actionbar ActionBar = Getactionbar ();
Actionbar. Sethomebuttonenabledfalse }
}
Note: use to SDK_INT
prevent legacy systems from performing new API work in this way only at Android 2.0 (API Level 5) and higher. Older versions will encounter run-time exceptions.
Once onCreate()
the execution is complete, the system calls onStart()
and onResume()
methods sequentially. Your activity will never reside in the "created" or "started" state. Technically, the activity becomes visible when it is called, but then the onStart()
onResume()
activity remains in the "continue" state until something happens that causes it to change, such as when a call is received, the user navigates to another activity, or the device screen is turned off.
In the rest of the next lesson, you'll see how other activity launches methods that are onStart()
onResume()
especially useful in your activity life cycle when used to resume activity from the pause or stop state.
Note: onCreate()
The method includes a savedInstanceState
parameter called, which is discussed in subsequent lessons about recreating the activity.
Figure 2. Another diagram of the activity lifecycle structure that focuses on the three major callbacks that the system calls in turn when creating a new instance of activity: onCreate()
, onStart()
and onResume()
. Once this sequence of callbacks is complete, the activity enters the "continue" state, where the user can interact with the activity until the user switches to another activity.
Destroying activity
When the activity's first life-cycle callback is onCreate()
, its recent callback is onDestroy()
. The system calls this method on your activity as the final signal that your activity instance is completely removed from system memory.
Most applications do not need to implement this method because the local class reference is destroyed with the activity, and your activity onPause()
should onStop()
perform most cleanup operations during and during. However, if your activity contains a onCreate()
background thread that you created during the period or other long-running resources that may cause memory leaks if they are not properly closed, you should onDestroy()
terminate them during the period.
@Override
public Ondestroy () {
super. //always call the superclass
//Stop method tracing that the activity started D Uring onCreate ()
Android.. Debug. Stopmethodtracing } /span>
Note: in all cases, the system is invoked onPause()
and onStop()
then called after onDestroy()
, with only one exception: when you onCreate()
call from within a method finish()
. In some cases, such as when your activity is running as a temporary decision tool to start another activity, you can onCreate()
call from within finish()
to destroy the activity. In this case, the system is called immediately without onDestroy()
invoking any other life-cycle methods.
Second management life cycle start activity