Activity lifecycle Function

Source: Internet
Author: User

Description: describes the Seven functions of the activity lifecycle.

Example: Add a button in mainactivity, touch the button, jump to otheractivity, and observe the output in the console.

Let's take a look at these functions:


Their process can look at the flowchart provided on the official website:


The following pyramid chart may be more intuitive:


It's boring to look at the picture. Let's see the effect of running the program first.

Step: 1. Rewrite the other six functions in mainactivity. Define an otheractivity class, add a button in activity_main, jump to otheractivity, and define a layout file other. XML for otheractivity. (This method has already been mentioned in the previous article and will not be repeated here .)

package com.away.b_02_lifecycle;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button button; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.Button);button.setOnClickListener(new ButtonListener());System.out.println("MainActivity:OnCreate");}class ButtonListener implements OnClickListener {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(MainActivity.this, OtherActivity.class);startActivity(intent);}}@Overrideprotected void onDestroy() {super.onDestroy();System.out.println("MainActivity:onDestroy");}@Overrideprotected void onPause() {super.onPause();System.out.println("MainActivity:onPause");}@Overrideprotected void onRestart() {super.onRestart();System.out.println("MainActivity:onRestart");}@Overrideprotected void onResume() {super.onResume();System.out.println("MainActivity:onResume");}@Overrideprotected void onStart() {super.onStart();System.out.println("MainActivity:onStart");}@Overrideprotected void onStop() {super.onStop();System.out.println("MainActivity:onStop");}}
2. re-write 7 functions in ohteractivity.

View the result directly: directly observe on the console, start mainactivity → otheractivity, and call the function.

Start mainactivity:


Click the button to jump to otheractivity:


When you click back to return to mainactivity:


Back stack is involved here:

1. Start the first activity, and the first activity object exists in the backstack.


2. Start the second activity and add the second activity object to the backstack.


3. Start the third activity and add the third activity object to the backstack. If you press the return key to return to the second activity, the third activity object in the backstack will be destroyed. Similarly, backstack follows the FIFO order.


Finished. It is not easy to be original in the middle of the night ~~ Good night.





Activity lifecycle Function

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.