preface

  • ActivityBelong toAndroidOne of the four components of
  • This article will be fully analyzedActivity, serve 1 portionActivityLearning guide, including its life cycle, startup mode, startup mode and so on, I hope you will like it.

directory


Definition 1.

An activity is a presentation component

It is one of the four components of Android: Activity, Service, BroadcastReceiver, and ContentProvider


2. The role

Display interface & interact with the user

  1. aActivityUsually an interface is the only one of the four components that can be perceived by the user
  2. Each activity is implemented as a separate class, & inherited from the activity base class
  3. ActivityThrough betweenIntentcommunicate

3. Life cycle

  • The details are shown below.

For more details, see Android Basics: the Activity lifecycle in 3 minutes


4. Start mode

  • ActivityThere are four boot modes, as follows

  • The differences between the four boot modes

For more details, see Android Basics: The most easy-to-understand Activity startup mode


5. Startup mode

  • Start theActivityThe main way is: explicitIntent& implicitIntent
  • Detailed introduction is as follows:

5.1 Explicit Intent (three types)

Intent = new Intent(this, secondActivity.class); startActivity(intent); Intent Intent = new Intent(); / / way 1: the package name + the name of the class / / parameters 1 = package name / / 2 = to start the fully qualified name of a class of intent. SetClassName (" com. Hc. Hctest ", "com. Hc. Hctest. SecondActivity"); // Method 2: Context+ class name // parameter 1 = Context, Can be directly introduced to the Activity / 2 = / parameters to start the fully qualified name of a class of intent. SetClassName (this, "com. Hc. Hctest. SecondActivity"); startActivity(intent); ComponentName () = new Intent(); ComponentName cn = new ComponentName("com.hc.hctest", "com.hc.hctest.SecondActivity"); intent.setComponent(cn); startActivity(intent);Copy the code

5.2 the implicit Intent

Intent = new Intent(); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addCategory("com.hc.second"); intent.setAction("com.hc.action"); startActivity(intent);Copy the code

5.3 Matching Rules

Android: The Trivia of Intent components (Introduction, Usage, etc.)


6. Start the process

The startup process of an Activity is as follows:

6.1 sketch

6.2 Description

When the request starts the Activity:

  1. LauncherThe process byBinderDrive to theActivityManagerServiceClass astartActivityRequests;
  2. ActivityManagerServiceUpon receiving the request, the class calls theActivityStackClass send startActivityThe request;
  3. ActivityStackClass records what to startActivityInformation & adjustmentActivityStack puts it at the top of the stack and passesBinderDriver willActivityThe startup information is passed toApplicationThreadIn the thread (i.eBinderThread)
  4. ApplicationThreadThread throughHandlerwillActivityIs sent to the main threadActivityThread
  5. The main threadActivityThreadClass receives the message & request and passesClassLoaderMechanism loads correspondingActivityClass, final callActivitytheThe onCreate (), and finally start complete

7. The cause of stagnation

The main reasons for Activity stagnation are as follows:

This is a comprehensive & detailed guide to memory optimization: Android performance optimization: Hand-in-hand with you to fully understand memory leaks & performance optimization solutions for Android: Those little things about Bitmap image resource optimization Android performance optimization: hand in hand with you to fully understand the drawing optimization Android performance optimization: layout optimization detailed analysis (including, explain)


8. Accelerated startup mode

The way to speed up an Activity is summarized as follows:


9. Cache mode (state saving)

  • Problem description

  • specify


10. Interaction between activities and fragments

  • There are interfaces, bundles, and broadcasts
  • Implement communication between Activity and Fragment (including Demo)

This concludes the tutorial on Activity, one of the four components of Android.


11. A summary

  • This article covered activities thoroughly, and you should now be familiar with activities

  • In my next post, I’ll cover Android. If you’re interested, keep an eye out for Carson_Ho’s Android development notes


Please give the top/comment a thumbs up! Because your encouragement is the biggest power that I write!