Explain singletast startup mode of Activity and how to use intent to pass values

  • 2020-10-23 21:13:05
  • OfStack

Four startup modes of Activity:

1. standard

Mode startup mode, Activity is created and placed on the task stack each time Activity is activated.

2. singleTop

If an instance of Activity happens to exist on the top of the task stack, the instance is reused, or a new instance is created and placed on the top of the task (even if the Activity instance already exists on the stack, as long as it is not on the top).

3. singleTask

If an instance of Activity is already on the stack, the instance is reused (onNewIntent() of the instance is called). When reused, it brings the instance back to the top of the stack, so the instance above it will be removed from the stack. If the instance does not exist in the stack, a new instance is created and placed on the stack.

4. singleInstance

Create the Activity instance in a new stack and have multiple applications share the Activity instance in the stack. Once an instance of the modified Activity exists in a stack, any application that reactivates the modified Activity will reuse the instance in the stack, which is equivalent to multiple applications sharing one application, and whoever activates the Activity will go into the same application.

Since there is a function of message push in the project, FunctionActivity will be turned on every time a message is pushed, in order to avoid it being turned on repeatedly, the startup mode of Activity will be changed to singleTask when the Activity appears several times during the retreat.

Starting the Activity multiple times calls the onNewIntent(Intent intent) method.

When activity passes data through intent, if activity is not started, the data of this intent will be obtained through getIntent() in the newly started activity. If the activity to be started already exists, then the intent obtained by the method getInten() is the original intent of the started activity. In other words, intent's data has not been updated. Thus, the data of intent obtained in the started activity is old data. To get new data from intent each time, call setIntent(intent) in the onNewIntent(Intent intent) method to set the latest intent from the intent method. As shown below:


 @Override
 protected void onNewIntent(Intent intent) {
 super.onNewIntent(intent);
 Log.e("tag", "onNewINtent To perform the ");
 setIntent(intent);
 String ringName = intent.getStringExtra("ringName");
 Log.e("tag", ringName+" Student: The value that was passed ");
 if (ringName != null) {
 pager.setCurrentItem(1);
 }
 }

Of course, if activity starts in standard, then a new activity is recreated each time. So intent is also up to date. You don't have to update this intent via setIntent.

In my project, there are 4 Fragment in FunctionActivity, so I can jump from other Activity to FunctionActivity without instantiation, and the latest intent cannot be obtained through getIntent () method, in order to solve this problem. Again in the onNewIntent method, the updated intent is passed by getIntent().putExtras(intent); Share out, as shown below:


@Override
 protected void onNewIntent(Intent intent) {
 super.onNewIntent(intent);
 Log.e("tag", "onNewINtent To perform the ");
 setIntent(intent);
 getIntent().putExtras(intent);
 }

This is called in the Fragment associated with it


@Override
 public void onResume() {
 super.onResume();
 //  The first 1 The next time you enter this page, the following method will not be executed because ringName is null
 String ringName = getActivity().getIntent().getStringExtra("ringName");
 if (ringName != null) {
 newSound.setText(ringName);
 Log.e("tag", ringName + " The value to save ");
 SharedPreferenceUtil.setString(getActivity(),
  SharedPreferenceUtil.RINGTONE_NAME, ringName);
 }
}

Notice here that when Fragment calls, 1 must be in the onResume method.


Related articles: