SoFunction
Updated on 2025-04-12

Basic introduction to activity from creation to display in Android

Preface

Speaking of Activity in Android, if you have done iOS development, Activity is similar to ViewController in iOS. Everything you can see in the application is placed in the activity. Activities are a relatively important thing in Android development and are the entrance to user interaction and data. The content to be introduced in this blog is the creation of activities, the jump of activities and the transmission of values.

ViewController in iOS also has its own life cycle. It is necessary to understand the life cycle of Activity or ViewController. This article will introduce to you in detail the relevant content about activity from creation to display in Android. We will share it for your reference and learning. I won’t say much below, let’s take a look at the detailed introduction together.

Activity is the most commonly used component we usually develop. It is necessary for us to understand the creation and display process of activity, which should be used as our reserve knowledge.

Creation of Activity

The process of creating and initializing the Activity is in the ActivityThread#performanceLaunchActivity method. In this method, there are the following key points.

  • Create an Activity
  • Activity#attach
  • Instrumentation#callActivityOnCreate
  • Activity#performStart
  • Instrumentation#callActivityOnPostCreate

This place allows you to see a small part of the Activity life cycle. We need to learn some of these points, and there are some very important operations in these points.

I won’t talk about the process of creating an Activity, just reflect it directly. Let's focus on the attachment method.

Activity#attach

The attachment part code is as follows

mWindow = new PhoneWindow(this, window);
(this);
(this);
(this);
().setPrivateFactory(this);

In the Activity attach method, a very important point is to initialize the Window. From here, you can see that the Window implementation class is PhoneWindow. The creation of PhoneWindow is very important for our subsequent operations.

Activity#onCreate

public void callActivityOnCreate(Activity activity, Bundle icicle,
  PersistableBundle persistentState) {
 prePerformCreate(activity);
 (icicle, persistentState);
 postPerformCreate(activity);
}

In this case, the onCreate method of activity will be called, which is very familiar to us in normal development. In onCreate, we call setContentView to fill the layout and perform some initialization operations

setContentView

When we reach the setContentView that we are quite familiar with, in the setContentView, the setContentView method of PhoneWindow will be called. Let's take a look at the setContentView of PhoneWindow

public void setContentView(int layoutResID) {
 // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
 // decor, when theme attributes and the like are crystalized. Do not check the feature
 // before this happens.
 if (mContentParent == null) {
  installDecor();
 } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
  ();
 }
 if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
  final Scene newScene = (mContentParent, layoutResID,
    getContext());
  transitionTo(newScene);
 } else {
  (layoutResID, mContentParent);
 }
 ();
 final Callback cb = getCallback();
 if (cb != null && !isDestroyed()) {
  ();
 }
 mContentParentExplicitlySet = true;
}

In the setContentView method of PhoneWindoe, the DecorView will be initialized and the layout we set is loaded into the contentparent. We won't talk about the specific logic of installDecor here.

resume process

In the ActivityThread#handleResumeActivity method, there are two key points.

  • performResumeActivity
  • Window#addView

The performanceResume of the activity will be called in the performanceResume Activity, onResume will be called in the performanceResume, and then enter the onresume declaration cycle.

Let's focus on addView and subsequent processing.

addView

(decor, l);

Here wm is WindowManager, which is initialized in the attach method by setting WindowManager. The corresponding instance is an instance of WindowManagerImpl. So, let’s look at the addView method of WindoeManageImpl. In this method, the addView method of WindowManagerGlobal is called directly, and the midpoint we care about is transferred. The most critical diam is the following lines.

root = new ViewRootImpl((), display);
(wparams);
(view);
(root);
(wparams);
(view, wparams, panelParentView);

First create a ViewRootImpl and then setView. The ViewRootImpl#setView method has a long code. We can find the requestLayout method, go in and take a look.

@Override
public void requestLayout() {
 if (!mHandlingLayoutInLayoutRequest) {
  checkThread();
  mLayoutRequested = true;
  scheduleTraversals();
 }
}

Here, the first thread check was performed.

void scheduleTraversals() {
 if (!mTraversalScheduled) {
  mTraversalScheduled = true;
  mTraversalBarrier = ().getQueue().postSyncBarrier();
  (
    Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
  if (!mUnbufferedInputDispatch) {
   scheduleConsumeBatchedInput();
  }
  notifyRendererOfFramePending();
  pokeDrawLockIfNeeded();
 }
}

Choreographer posts a Callback, which is the core of view refresh. Let's take a look at the run method of TraversalRunnable.

final class TraversalRunnable implements Runnable {
 @Override
 public void run() {
  doTraversal();
 }
}
void doTraversal() {
 if (mTraversalScheduled) {
  mTraversalScheduled = false;
  ().getQueue().removeSyncBarrier(mTraversalBarrier);
  if (mProfile) {
   ("ViewAncestor");
  }
  performTraversals();
  if (mProfile) {
   ();
   mProfile = false;
  }
 }
}

In doTraversal, the performTraversals method will be called again. Let’s take a look at what the performTraversals method does. This method is very, very long, but in this method, there are very critical methods such as performMeasure, performLayout, and performDraw. At this point, the three major processes of entering the View are displayed in front of us.

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.