Home>

Create the activity instance in a new stack, and let multiple applications share the activity instance in the stack. Once an instance of the mode-changing activity exists in a stack,Any application that reactivates the activity will reuse the instances in the stack.The effect is equivalent to multiple applications sharing one application,Whoever activates the activity will enter the same application

The four startup modes of the activity:

1. standard

Mode start mode,Every time the activity is activated, the activity is created and placed in the task stack.

2. singletop

If there is an instance of the activity at the top of the task's stack, the instance is reused.Otherwise, a new instance will be created and placed on the top of the stack (even if the activity instance already exists in the stack, as long as it is not on the top of the stack,Will create instances).

3. singletask

If there is already an instance of the activity in the stack, then reuse the instance (the instance's onnewintent () is called). When reused, the instance will be returned 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 will be created and placed on the stack.

4. singleinstance

Create the activity instance in a new stack, and let multiple applications share the activity instance in the stack. Once an instance of the mode-changing activity exists in a stack,Any application that reactivates the activity will reuse the instances in the stack.The effect is equivalent to multiple applications sharing one application,Whoever activates the activity will enter the same application.

Because in the project,There is a message push function,Push one message at a time,Will open the functionactivity, so in order to avoid repeatedly opening it,When backing up,The activity appears multiple times, and the startup mode of the activity becomes singletask.

In this way, the onnewintent (intent intent) method will be called multiple times after starting the activity.

When an activity passes data through an intent,If the activity is not started, the data of this intent will be obtained by getintent () in the newly started activity. If the activity to be started already exists,At this time, the intent obtained by the getinten () method is the original intent of the started activity. In other words, the data of the intent is not updated.In this way, the data of the intent obtained in the launched activity is the old data.If i want to get new data from the intent each time, you need to call setintent (intent) in the onnewintent (intent intent) method to set the latest intent. This is as follows:

@override
 protected void onnewintent (intent intent) {
 super.onnewintent (intent);
 log.e ("tag", "onnewintent executed");
 setintent (intent);
 string ringname=intent.getstringextra ("ringname");
 log.e ("tag", ringname + "Passed value");
 if (ringname!=null) {
 pager.setcurrentitem (1);
 }
 }

Of course, if the activation mode of the activity is standard, then a new activity is re-created every time. This way the intent is also the latest.There is no need to update this intent through setintent.

In my project,There are four fragments in the functionactivity, so that if I jump from another activity to the functionactivity, it will not be instantiated.The latest intent cannot be obtained through the getintent () method, in order to solve this problem.Still in the onnewintent method, share the updated intent through getintent (). Putextras (intent);As follows:

@override
 protected void onnewintent (intent intent) {
 super.onnewintent (intent);
 log.e ("tag", "onnewintent executed");
 setintent (intent);
 getintent (). putextras (intent);
 }

So in the fragment associated with it, you can call

@override
 public void onresume () {
 super.onresume ();
 //Enter this page for the first time,The following methods are not executed,Because ringname is null
 string ringname=getactivity (). getintent (). getstringextra ("ringname");
 if (ringname!=null) {
 newsound.settext (ringname);
 log.e ("tag", ringname + "value to save");
 sharedpreferenceutil.setstring (getactivity (),  sharedpreferenceutil.ringtone_name, ringname);
 }
}

Note that when fragment is called here,Must be in the onresume method.

  • Previous Very comprehensive summary of PHP datetime operations
  • Next Python's method for periodically grabbing webpage content