Official explanation: Activity is a component of an application that provides an area on the screen that allows users to do some interactive operations on it, such as making calls, taking pictures, sending emails, or displaying a map! Activity can be understood as a window that draws the user interface, and this window can fill the entire screen, or it may be smaller than the screen or float on top of other windows!
Explanation: The blog summary is obtained from watching videos/documents by yourself
Table of contents
1. The life cycle of Android activities
2. Four startup modes of Android activities
3. Several ways to start Android activities
4. Several common points of Android activities
4.1 Data transfer between activities
4.2 Interaction between Multiple Activities
4.3 Close all activities at any time
1. The life cycle of Android activities
1. When calling an Activity for the first time, it will execute: onCreate method
2. When the Activity is in the visible state, it will call: onStart method
3. When the Activity can get the user focus, it will call: onResume method
4. When the Activity is blocked, it will call: onPause method
5. When the Activity is in an invisible state, it will call: onStop method
6. When the Activity is not destroyed, when the Activity is called again, it will call: onRestart method
7. When the Activity is destroyed, it will call: onDestory method
- Let's write a case to see
liucheng jumps to FinishActivity: click the jump button in StartActivity, jump to FinishActivity, and then click the finish button to execute finish
-
The code is simple, just paste the StartActivity code
public class StartActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "StartActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG,"onCreate"); setContentView(R.layout.activity_start); findViewById(R.id.start_button).setOnClickListener(this); } @Override public void onClick(View view) { startActivity(new Intent(this, FinishActivity.class)); } @Override protected void onStart() { super.onStart(); Log.d(TAG,"onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG,"onResume"); } @Override protected void onPause() { super.onPause(); Log.d(TAG,"onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG,"onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG,"onDestroy"); } }
- Process: StartActivity——FinishActivity——StartActivity——press the home keyboard to return to the main interface
- Notice
1. The prerequisites for onPause() and onStop() to be called are: a new Activity is opened, and the former is a state where the old Activity is still visible; the latter is that the old Activity is no longer visible!
2. AlertDialog and PopWindow will not trigger the above two callback methods~
2. Four startup modes of Android activities
- The management of Activity in Android is also similar to the Stack in Java.
Last-in-first-out (LIFO), common operations push (push), pop (pop), the top is called the top of the stack, and the bottom is called the bottom of the stack
- Stack in Android manages Activity like this
When switching to a new Activity, the Activity will be pushed onto the stack and become the top of the stack! And when the user clicks the Back button, the Activity at the top of the stack is popped out of the stack, and the next Activity comes to the top of the stack!
- standard mode: Each call to the startActivity() method will generate a new instance
- singleTop mode: If there is already an instance at the top of the Activity stack, no new instance will be generated, but the newInstance() method in the Activity will be called. If not at the top of the stack, a new instance will be created
- singleTask mode: This instance will be generated in a new task, and this will be returned every time it is called, and no new instance will be generated
- singleInstance mode: only one Activity instance will be created, and a new task stack will be used to load the Activity instance
- Note: Let’s take the singleInstance mode as an example. Suppose we start three activities, ActivityA->ActivityB->ActivityC. Except B is the singleInstance mode, the others are the default. When we return to the C Activity interface, we return to A, because B It is an independent task stack; only: after finishing in ActivityC, it will return to the interface of ActivityA, and after finishing in ActivityA, it will return to the interface of ActivityB
3. Several ways to start Android activities
- show start
- Explicit start: start by package name
startActivity(new Intent(current Act.this,to start Act.class));
- ComponentName via Intent
ComponentName cn = new ComponentName("current Act The fully qualified class name of","start up Act The fully qualified class name of") ; Intent intent = new Intent() ; intent.setComponent(cn) ; startActivity(intent) ;
- Implicit start: through the Action,Category or data of the Intent-filter, as long as the Activity has a corresponding action and Category, it will be started. and then provide the user with the option
<activity android:name="com.android.activityActivity" android:label="Huawei" > <intent-filter> <action android:name="android.intent.action.SECONDACTIVITY_START" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Intent intent = new Intent(); intent.setAction("android.intent.action.SECONDACTIVITY_START"); intent.addCategory(Intent.CATEGORY_DEFAULT);
4. Several common points of Android activities
4.1 Data transfer between activities
4.2 Interaction between Multiple Activities
4.3 Close all activities at any time
public class ActivityCollector { public static LinkedList<Activity> activities = new LinkedList<Activity>(); public static void addActivity(Activity activity) { activities.add(activity); } public static void removeActivity(Activity activity) { activities.remove(activity); } public static void finishAll() { for(Activity activity:activities) { if(!activity.isFinishing()) { activity.finish(); } } } }