Java – How do I start an activity from a background application above 26 APIs?

How do I start an activity from a background application above 26 APIs?… here is a solution to the problem.

How do I start an activity from a background application above 26 APIs?

I want to open my activity from a service or broadcast while my application is in the background.

I tried the code below

Intent intent = new Intent("android.intent.action.MAIN");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

It works fine on the following Android 26 APIs. But for 26Api and above, it does not start the activity.

It provides flow information in Android LogCat

Timeline: Timeline: Activity_launch_request

Solution

Android 10 adds a limit to “start a new activity from the background”. View documentation: Restrictions on starting activities from the background

For more details on the reasons for these restrictions, check out Chet Haase’s keynote. GFM2019-Keynote-Chet Haase

Related Problems and Solutions