Activity is one of the four most basic components of Android (Activity Activity Activity, Service Service, ContentProvider Content Provider, BroadcastReceiver Broadcasting). Activity is mainly responsible for interaction with users and is the knowledge that every Android development must master.
This article mainly introduces some of the knowledge points in Android development, including the following:
- Introduction to Activity Life Cycle
- Activity must be registered in AndroidMainfest.xml
- How to Start Activity
- Start Activity with Return Value
- Activity End Method
- Activity State Save, Recovery Method
- Examples frequently asked in interviews
1. Activity life cycle introduction
First we need to understand the inheritance relationship of Activity.
Activity Inheritance Relationship
Activity inheritance relationships are as follows:
java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.view.ContextThemeWrapper ↳ android.app.Activity
After understanding the inheritance relationship of an Activity, we begin to understand its claim cycle, which directly affects the interaction with users.
Activity life cycle
Activity life cycle diagram is as follows:
Activity Life Cycle Diagram
Activity Lifecycle Callback Method in Code
Activity life cycle callback methods are as follows:
Activity Life Cycle Code Callback Method
Activity 4 Life Cycle States
The four common life cycle states of an Activity are as follows:
-
Active Running State
-
Pause pause state
-
Stop Stop State
-
Killed's extinction status
2. Activity must be registered in AndroidMainfest.xml
Activity is one of the four components that Android stipulates must be registered in AndroidMainfest.xml. If an activity is not registered, it will cause an App Crash error.
ActivityNotFoundException
If the Activity is not registered in AndroidMainfest.xml, the app will report an error. The main exception information is as follows:
ActivityNotFoundException
For example, the following error message:
//Tip No Activity class declaration android.content.ActivityNotFoundException found in AndroidMainfest.xml: Unable to find explicit Activity class //Specific class name, package name as follows: {com.wj.utils/com.wj.utils.basewidget.BaseButtonMethods}; have you declared this activity in your AndroidManifest.xml?
ActivityNotFoundException exception Log analysis
Activity is registered as follows:
<manifest ... > <application ... > <activity android:name=".BaseButtonMethods" /> ... </application ... > ...</manifest >
Common Action Labels
android.action.MAIN:
Represents the entry main method class for the current App.
android.intent.category.LAUNCHER:
Indicates that this Action is scanned by Launcher and can be displayed on Launcher's Icon list. If this Action is removed, the Icon of this app cannot be viewed in Launcher species.
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>
3. How to Start Activity
Show Startup
Intent intent = new Intent(ActivityMethods.this, OtherActivity.class) startActivity(intent);
Implicit Start
Intent intent = new Intent("string_action");// intent.setAction("string_action");startActivity(intent);
4. Start Activity with Return Value
Launch Activity Method with Return Value
Intent intent = new Intent(); intent.setClass(ActivityMethods.this, OtherActivity.class); startActivityForResult(intent, mRequestCode);
Callback method for handling return values
// Method to get the result returned by Activity @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == mRequestCode && resultCode == mResultCode) { String result = data.getStringExtra("str_set_result"); Toast.makeText(this, "result :" + result, Toast.LENGTH_SHORT).show(); } }
The method by which the Activity is started to set the return value
int resultCode = 101; Intent intent = new Intent(); intent.putExtra("str_set_result", "With returned results Activity"); setResult(resultCode, intent);
5. Activity End Method
If you want to end the current Activity, you can call the method once
finish(); //Or end Activity based on request code finishActivity(int requestCode);
6. Activity state saving, recovery methods
When an Activity exits abnormally, it automatically saves some data, but if it is important data for the app, manually save Bundle-type data in the code to prevent the Activity from saving unsatisfactory data.
Activity State Save and Restore
The cycle diagram for Activity state preservation and recovery is as follows:
Activity Status Save Life Cycle Diagram
Callback methods for Activity state preservation and recovery
The callback methods for Activity state preservation and recovery are as follows:
// Activity's method of recovering data, often in the oncreate method @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, "----onRestoreInstanceState----"); } // Activity's method of saving data, often in the onPause method @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); Log.i(TAG, "----onSaveInstanceState----"); }
7. Examples frequently asked in interviews
Activity A starts Activity B and then returns to A to briefly describe its life cycle?
- First A starts with the following declaration cycle:
01-02 21:25:22.357 21225-21225/com.android.program.programandroid I/ActivityMethods wjwj:: ----onCreate----01-02 21:25:22.396 21225-21225/com.android.program.programandroid I/ActivityMethods wjwj:: ----onStart----01-02 21:25:22.402 21225-21225/com.android.program.programandroid I/ActivityMethods wjwj:: ----onResume----
- Click Button in A, jump to B, and declare the cycle relationship as follows:
01-02 21:28:30.617 23845-23845/com.android.program.programandroid I/ActivityMethods wjwj:: ----onPause----01-02 21:28:30.723 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onCreate----01-02 21:28:30.729 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onStart----01-02 21:28:30.738 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onResume----01-02 21:28:31.320 23845-23845/com.android.program.programandroid I/ActivityMethods wjwj:: ----onStop----
- End B, return A, the life cycle is as follows:
01-02 21:29:38.646 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onPause----01-02 21:29:38.668 23845-23845/com.android.program.programandroid I/ActivityMethods wjwj:: ----onRestart----01-02 21:29:38.672 23845-23845/com.android.program.programandroid I/ActivityMethods wjwj:: ----onStart----01-02 21:29:38.674 23845-23845/com.android.program.programandroid I/ActivityMethods wjwj:: ----onResume----01-02 21:29:39.058 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onStop----01-02 21:29:39.059 23845-23845/com.android.program.programandroid I/OtherActivity wjwj:: ----onDestroy----
Summary:
Activity A starts Activity B and gives a brief description of its life cycle?The general flow is as follows:
Activity A starts Activity B and then returns to the A life cycle diagram
This is the end of this article. If something is wrong, you are welcome to make suggestions and corrections.At the same time, we look forward to your attention, thank you for your reading, thank you!