Android official Getting Started document [12] Start an activity

Source: Internet
Author: User

Android official Getting Started document [12] Start an activity
Starting an Activity
Start an activity


This lesson teaches
1.Understand the Lifecycle callbacks
2.Specify Your App ' s Launcher Activity
3.Create a New Instance
4.Destroy the Activity

This lesson teaches you
1. Understanding Life cycle Callbacks
2. Specify the startup activity for your application
3. Create a new instance
4. Destruction activities

You should also read
? Activities

You should also read
? activities

Try it Out
Give it a try.

Download the Demo
Activitylifecycle.zip
Download Demo
Activitylifecycle.zip

Unlike other programming paradigms in which apps is launched with a main () method, the Android system initiates code in a n Activity instance by invoking specific callback methods the correspond to specific stages of its lifecycle. There is a sequence of callback methods, start up an activity and a sequence of callback methods the tear down an act Ivity.
Unlike the other programming paradigms introduced in the application with a main () method, the Android system launches the active instance of the code by invoking a specific callback method that corresponds to a specific stage of its life cycle. Another problem is the initiation of an activity that pushes the sequence of callback methods of the active callback method sequence.

This lesson provides a overview of the most important lifecycle methods and shows what to handle the first lifecycle C Allback that creates a new instance of your activity.
This lesson provides an overview of the most important life cycle methods and tells you how to handle the first life cycle callback to create a new instance of your activity.

Understand the Lifecycle callbacks
Understanding Life cycle Callbacks


--------------------------------------------------------------------------------

During the life of an activity, the system calls a core set of lifecycle methods in a sequence similar to a step pyramid. That's, each stage of the activity lifecycle was a separate step on the pyramid. As the system creates a new activity instance, each callback method moves the activity state one step toward the top. The top of the pyramid is the "at which" the activity is running in the foreground and the user can interact with it.
In the lifetime of an activity, the system calls the core in a life-cycle approach similar to the step pyramid sequence set. That is, each stage of the activity's life cycle is a separate step in the pyramid. Since the system creates a new activity instance, each callback method moves the active state one step above. The top of the pyramid is the point at which the activity runs in the foreground, and the user can interact with it.

As the user begins to leave the activity, the system calls other methods that move the activity state back down the Pyrami D in order to dismantle the activity. In some cases, the activity would move only if part is down the pyramid and wait (such as if the user switches to another a PP), from which point the activity can move back to the top (if the user returns to the activity) and resume where the R left off.
When the user begins to leave the activity, the system calls the moving active state back down the pyramid in order to remove the other method of the activity. In some cases, the activity will move only one part of the way down the pyramid and wait, the activity from that point can be moved back to the top (when the user switches to another application, such as) (if the user returns to activity) and resumes where the user left off.

Figure 1. A simplified illustration of the Activity lifecycle, expressed as a step pyramid. This shows how, for every callback used to take the activity a step toward the resumed state at the top, there ' s a Callbac K method that takes the activity a step down. The activity can also return to the resumed state from the Paused and Stopped state.
Figure 1. A simplified diagram of the life cycle, represented as a step pyramid. This shows how the steps for capturing activity on the restored state are at the top of the callback, there is a callback method, and it's active to step down. Activities can also be returned from the paused and stopped states to the recovery state.

Depending on the complexity of your activity, you probably don ' t need to implement all the lifecycle methods. However, it's important that's understand each one and implement those that ensure your app behaves the the-the-the-the-the-the-' expect . Implementing your Activity lifecycle methods properly ensures your app behaves well in several ways, including that it:
? Does not crash if the user receives a phone call or switches to another app while using your app.
? Does not consume valuable system resources, the user is not actively using it.
? Does not lose the user's progress if they leave your app and return to it at a later time.
? Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation.
Depending on the complexity of your activity, you may not need to implement all life cycle methods. However, it is important that you understand every one and the way that you implement the expectations of your users to ensure that your application behaves. Implement the life cycle method of your activity correctly, ensure that your application behaves as well as in several ways, including it:
? If a user receives a call or switches to another application using your application, it does not crash.
Do not consume valuable system resources when the user is not actively using it.
? Do not lose the user's progress if they leave your application and return to it at a later time.
? Do not crash or lose the user's progress when the screen rotates between landscape and portrait.

resumed
in the", the activity was in the foreground and the user can interact wit H it. (Also sometimes referred to as the ' running ' state.)
Paused
In the "the activity is partially obscured by another activity-the other activity that's in the Foreg Round is semi-transparent or doesn ' t cover the entire screen. The paused activity does not receive user input and cannot execute any code.
Stopped
In the "The activity is completely hidden" and "not" visible to the user; it's considered to being in the Background. While stopped, the activity instance and all their state information such as member variables are retained, but it cannot exe Cute any code.
Restore
In this state, the activity is in the foreground and the user can interact with it. (sometimes referred to as the "running" state).
Pause
In this state, the activity part is the other activity of the other activities this is in the foreground is translucent or does not cover the entire screen mask. Pausing an activity does not accept user input and cannot execute any code.
Stop
In this state, the activity is completely hidden, not visible to the user; It is considered to be in the background. While stopping, the active instance and all its state information, such as member variables, are persisted but cannot execute any code.

The other states (Created and Started) is transient and the system quickly moves from them to the next state by calling T He next lifecycle callback method. That was, after the system calls OnCreate (), it quickly calls OnStart (), which was quickly followed by Onresume ().
Other states (Create and start) are ephemeral, and the system will quickly move from them to the next state by invoking the next periodic callback method. That is, after the system calls the OnCreate (), it quickly calls OnStart (), which is immediately followed by Onresume ().

That's it for the basic activity lifecycle. Now you'll start learning about some of the specific lifecycle behaviors.
This is the life cycle of its basic activities. Now you will begin to learn some specific life cycle behaviors.

Specify Your App ' s Launcher Activity
Specify the startup activity for the application


--------------------------------------------------------------------------------

When the user selects your app icon from the Home screen, the system calls the OnCreate () method for the Activity in your The app, "VE declared to be the" launcher "(or" main ") activity. This is the activity of the serves as the main entry point to your app's user interface.
When the user is from the application icon in the Home screen, the system invokes the activity of the OnCreate () method in your application that you have declared as "launcher (initiator)" (or "main"). This is activity as the main entry point to the user interface of your application.

You can define which activity to use as the main activity in the Android manifest file, Androidmanifest.xml, which are at t He root of your project directory.
You can define the activity to be used in Android manifest files, Androidmanifest.xml, which are the main activities in the root directory of your project directory.

The main activity for your app must is declared in the manifest with a <intent-filter> that includes the main actio N and LAUNCHER category. For example:
The primary activity for your application must be declared in manifest with the intent filter <intent-filter>, which includes the main action and launcher categories. For example:

Note:when Create a new Android project with the Android SDK tools, the default project files include an Activity clas s that's declared in the manifest with this filter.
Note: When you create an Android SDK tool, a new Android project, the default project file includes the activity class for the declaration of this filter in the manifest.

If either the MAIN action or LAUNCHER category is declared for one of your activities and then your app icon would not AP Pear in the Home screen ' s list of apps.
If one of the activities of the two main action or launcher category is not declared, then your application icon will not appear in the list on the main screen of the application.

Create a New Instance
Create a new instance


--------------------------------------------------------------------------------

Most apps include several different activities, the user to perform different actions. Whether an activity was the main activity that's created when the user clicks your app icon or a different activity that yo ur app starts in response to a user action, the system creates every new instance of Activity by calling its onCreate () Me Thod.
Most applications include several different activities that allow the user to perform different actions. Regardless of an activity, when a user clicks on your application icon, or your application begins to respond to the main activity created by the different activities of the user operation, the system creates each new instance of the activity by calling its OnCreate () method.

You must implement the OnCreate () method to perform basic application startup logic, should happen only once for the E Ntire life of the activity. For example, your implementation of onCreate () should define the user interface and possibly instantiate some class-scope Variables.
You must implement the OnCreate () method to perform the startup logic that should only occur once for the entire life of the active application. For example, you should define the user interface in the OnCreate () of your execution, and possibly instantiate variables within the class scope.

For example, the following example of the OnCreate () method shows some code this performs some fundamental setup for the A Ctivity, such as declaring the user interface (defined in an XML layout file), defining member variables, and configuring Some of the UI.
For example, the following example of the OnCreate () method shows some code that performs some basic settings for activities, such as announcing the user interface (defined in an XML layout file), defining member variables, and configuring some UI.

TextView Mtextview; Member variable for text view in the layout

@Override
public void OnCreate (Bundle savedinstancestate) {
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. Honeycomb) {
For the main activity, make sure the app icon in the action Bar
does not behave as a button
ActionBar ActionBar = Getactionbar ();
Actionbar.sethomebuttonenabled (FALSE);
}
}

Caution:using the Sdk_int to prevent older systems from executing new APIs works in this ' on Android 2.0 (API Level 5) and higher only. Older versions would encounter a runtime exception.
Note: Use Sdk_int to prevent the old system from performing new APIs in this way in Android2.0 (API Level 5) and later versions. The old version encounters a run-time exception.

once the OnCreate () finishes execution, the system calls the OnStart () and Onresume () met Hods in quick succession. Your activity never resides in the Created or Started states. Technically, the activity becomes visible to the user when OnStart () was called, but Onresume () quickly follows and the Act Ivity remains in the resumed state until something occurs to change this, such as when a phone call is received, the user Navigates to another activity, or the device screen turns off.
Once the OnCreate () execution is complete, the system calls the fast continuous onstart () and Onresume () methods. Your activity never resides in the Create or start state. Technically, the activity becomes visible to the user when OnStart () is called, but the Onresume () is quickly as follows, the activity remains in the state until the thing changes, when the phone call is received, for example, the user navigates to another activity, or the device screen is turned off.

In the other lessons this follow, you'll see how the other start up methods, OnStart () and Onresume (), is useful during Y Our activity's lifecycle when used to resume the activity from the Paused or Stopped states.
In the next other courses, you will see how other Startup methods, ONSTART () and Onresume (), are used when resuming activity from a paused or stopped state are very useful in the life cycle of your activity.

Note:the onCreate () method includes a parameter called Savedinstancestate that's discussed in the latter lesson about REC Reating an Activity.
Note: the OnCreate () method includes a parameter named Savedinstancestate that is discussed in the following lesson about the re-creation of the activity.

Figure 2. Another illustration of the activity lifecycle structure with a emphasis on the three main callbacks this system call s in sequence when creating a new instance of the Activity:oncreate (), OnStart (), and Onresume (). Once This sequence of callbacks complete, the activity reaches the resumed state where users can interact with the Activit Y until they switch to a different activity.
Figure 2. The focus of the activity cycle structure is to generate a new instance of the activity in sequence when the system calls another example of the three main callbacks: OnCreate (), ONSTART (), and Onresume (). Once the callback sequence is complete, the activity continues to reach a state where the user can interact with the activity until they transition to a different activity.

Destroy the Activity
Destruction activities


--------------------------------------------------------------------------------

While the activity's first lifecycle callback is OnCreate (), it very last callback is OnDestroy (). The system calls this method on your activity as the final signal, your activity instance is being completely removed From the system memory.
The oncreate of the first life-cycle callback for the activity is (), and its last callback is OnDestroy (). The system calls your activity as the final signal for the active instance to be completely removed from system memory by this method.

Most apps don ' t need to implement this method because local class references is destroyed with the activity and your ACTI Vity should perform most cleanup during onPause () and OnStop (). However, if your activity includes background threads that you created during onCreate () or other long-running resources T Hat could potentially leak memory if not properly closed, you should kill them during OnDestroy ().
Most applications do not need to implement this method because the local class references the activity that was destroyed, your activity should be in OnPause () and the OnStop () at the completion of the vast majority of cleanup. However, if your activity includes your oncreate () or other long-running resources that may cause a memory leak, if the background thread created during the process is not properly closed, you should kill them in the OnDestroy ().

@Override
public void OnDestroy () {
    Super.ondestroy ();  /Superclass
   
   //Stop method tracing that the activity Started during onCreate ()
    android.os.Debug.stopMethodTracing ();
}

Note:the system calls OnDestroy () after it had already called onPause () and OnStop () in all situations except One:when y OU call Finish () from within the OnCreate () method. In some cases, such as when your activity operates as a temporary decision maker to launch another activity, you might cal L finish () from within OnCreate () to destroy the activity. In this case, the system immediately calls OnDestroy () without calling any of the other lifecycle methods.
Note: After the system calls the OnDestroy (), it is already known as OnPause () and the OnStop () in all cases except one: when you call from the OnCreate () method to complete (). In some cases, such as when you launch another activity as a temporary decision maker, you can call within the OnCreate () to complete () from destroying the activity. In this case, the system immediately calls the OnDestroy () without invoking any other life-cycle methods.

Next:pausing and Resuming an Activity
Next: Paused and resumed activities

This article is translated from: https://developer.android.com/training/basics/activity-lifecycle/starting.html

Android official Getting Started document [12] start an activity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.