I have encountered this problem for a long time. Some solutions on the Internet are incomplete. This record is specially made:
The following method cannot be used to switch the activity running in the background to the foreground!
1 Intent i = new Intent();2 i.setClass(this, MainActivity.class);3 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);4 startActivity(i);
You must replace I with getapplicationcontext () to run mainactivity. However, instead of switching the mainactivity running in the background to the foreground, a new task is created to run mainactivity.
The following code must be used:
1 Intent start = new Intent(getApplicationContext(),MainActivity.class); 2 start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);3 startActivity(start);
In addition, the following red font configurations are required for the corresponding mainactivity.
<activityandroid:name=".MainActivity"android:label="@string/app_name" android:launchMode="singleTop">
In order to re-sort the mainactivity running in the background to run in the foreground.
Bringing the activity to foreground switch the activity to the foreground