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