Bringing the activity to foreground switch the activity to the foreground

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.