First, the function of Activity

  1. Layout file
setContentView()
Copy the code
  1. Lifecycle callback

  1. OnCreate (), the callback to initialize the Activity when it is first created (loading the layout)
  2. OnStart (), called when the user is visible
  3. OnResume (), which gets focus and is called when interacting with the user
  4. OnPause (), called when focus is lost (non-time-consuming operation)
  5. OnStop (), called when the user is not visible
  6. OnDestory (), called when the Activity is destroyed, frees resources
  7. OnNewIntent (),singleTop,singleTask mode is called when the Activity is restarted
  8. OnSaveInstanceState (), the Activity saves the user’s data when the exception is destroyed (vertical/horizontal switching)
  9. OnRestoreInstanceState (), Activity destroy restore user data on restart (vertical screen switch)
public class MainActivity extends AppCompatActivity { private final String TAG= MainActivity.class.getSimpleName(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG,"onCreate"); setContentView(R.layout.activity_main); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Log.i(TAG,"onNewIntent"); } @Override protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i(TAG,"onRestoreInstanceState"); } @Override protected void onStart() { super.onStart(); Log.i(TAG,"onStart"); } @Override protected void onResume() { super.onResume(); Log.i(TAG,"onResume"); } @Override protected void onPause() { super.onPause(); Log.i(TAG,"onPause"); } @Override protected void onStop() { super.onStop(); Log.i(TAG,"onStop"); } @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); Log.i(TAG,"onSaveInstanceState"); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG,"onDestroy"); }}Copy the code

Two, basic use

  1. The Manifest configuration
<activity android:name=".act.ActivityA" />
<activity android:name=".act.ActivityB">
  <intent-filter>
    <action android:name="com.my.study.activitya" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>
Copy the code
  1. intent-filter
  • The initial Activity
<intent-filter>
  <action android:name="android.intent.action.MAIN" />
  <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Copy the code
  • The launcher application
<intent-filter>
  <category android:name="android.intent.category.HOME" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Copy the code
  1. Start the
  • Explicit start
Intent intent=new Intent(ActivityA.this,ActivityB.class);
startActivity(intent);
Copy the code
  • Implicit start
Intent intent=new Intent();
intent.setAction("com.my.study.activitya");
startActivity(intent);
Copy the code
  1. Shut down
finish();
Copy the code
  1. Start ActivityA ActivityB
03-16 10:23:58.428 2168-2168/com.my. Study I/ActivityB: onPause 03-16 10:23:58.448 2168-2168/com.my. Study I/ActivityB: onPause 03-16 10:23:58.448 2168-2168/com.my. OnCreate 03-16 10:23:58.464 2168-2168/com.my. Study I/ActivityB: OnStart 03-16 10:23:58.464 2168-2168/com.my. Study I/ActivityB: OnResume 03-16 10:23:59.024 2168-2168/com.my. Study I/ActivityA: onStopCopy the code
  1. ActivityB return ActivityA
03-16 10:26:41.041 2168-2168/com.my. Study I/ActivityB: onPause 03-16 10:26:41.053 2168-2168/com.my. Study I/ActivityA: onPause 03-16 10:26:41.053 OnStart 03-16 10:26:41.053 2168-2168/com.my. Study I/ActivityA: OnResume 03-16 10:26:41.529 2168-2168/com.my. Study I/ActivityB: OnStop 03-16 10:26:41.529 2168-2168/com.my. Study I/ActivityB: onDestroyCopy the code

Task, Back Stack

  • One Task corresponds to one Back Stack
  • A Back Stack contains multiple activities
  • An APP can have multiple tasks
  • The default package name is Task, which can be specified based on the taskAffinify property

Fourth, the launchMode

<activity android:name=".mainActivity "Android :launchMode="singleTask" </activity>Copy the code
  • Standard, which recreates the Activity instance each time it starts

  • SingleTop, if the Activity is at the top of the stack of the current task, onNewIntent() is called instead of being recreated

The Activity is reused at the top of the stack for the current task

The Activity is not on the top of the stack of the current task and is recreated

  • SingleTask: allows only one instance of the Activity to exist in the system, creates an instance if it does not exist, and pushes all activities above the instance off the stack and calls onNewIntent(). Suitable as a program entry

  • SingleInstance, where the Task returns only one instance of this Activity on the stack

Five, the Intent of flag

Intent intent=new Intent(ActivityB.this, MainActivity.class); / / add flag intent intent. AddFlags (intent. FLAG_ACTIVITY_NEW_TASK | intent. FLAG_ACTIVITY_CLEAR_TASK; startActivity(intent);Copy the code
  • FLAG_ACTIVITY_NEW_TASK

When the Intent object contains this flag, the system looks for or creates a new task to place the target Activity in, matching the taskAffinity attribute of the target Activity. If a task with the same taskAffinity is found, If the task search fails, a new task is created and the taskAffinity of the task is set to the taskActivity of the target Activity, placing the target Activity in the task. Note that if the taskAffinity of all activities in the same application uses the default value or is set to the same value, it does not make sense to use this flag for jumps between activities within the application, since the current application task is the best host for the target Activity.

  • FLAG_ACTIVITY_SINGLE_TOP

When a target Activity instance exists in the Task and is at the top of the stack, instead of creating a new one, use the instance.

  • FLAG_ACTIVITY_CLEAR_TOP

When an Intent object contains this flag, if an Activity instance is found on the stack, the Activity above it is cleared to the top of the stack. With the default “Standard” launch mode, FLAG_ACTIVITY_SINGLE_TOP will be closed and rebuilt if the Intent is not used, or an existing instance will be used. For other launch modes, FLAG_ACTIVITY_SINGLE_TOP is no longer required. Instead, it uses an existing instance where the Intent is passed to onNewIntent().

  • FLAG_ACTIVITY_CLEAR_TASK

If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK. Tasks associated with this Activity are cleared before starting and can only be used with FLAG_ACTIVITY_NEW_TASK.