Android: how to resume application / activity from BroadcastReceiver?

If my procedure is as follows:

  • Starting Activity A β†’ Activity B
  • Click the Home button.
  • Click the app again.

Result: activity B is displayed (it resumes ).

  • Starting Activity A β†’ Activity B
  • Click the back button.
  • Click the app again.

Result: activity β€œA” is displayed (it restarts ).

I want to do the same from BroadcastReceiver.

  • Starting Activity A β†’ Activity B
  • Click the Home button.
  • BroadcastReceiver receives the broadcast and wants to β€œresume” the application.

Expected Result: Activity B is displayed.

I want to do the same from BroadcastReceiver.

  • Starting Activity A β†’ Activity B
  • Click the back button.
  • BroadcastReceiver receives broadcast and wants to "restart" the application.

Current result: Activity A is displayed.

The following code does not fulfill what I expect:

public void onReceive(Context context, Intent intent) { Intent i = new Intent(context, ActivityA.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } 

I also tried "Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY", but no luck.

+4
source share
2 answers

Gosh, I did it!

Thanks for the other answers you provided to you, but they were not what I was looking for.

This will complete the task:

 Intent i = getPackageManager().getLaunchIntentForPackage("com.your.package.name"); i.setFlags(0); i.setPackage(null); startActivity(i); 
+4
source

check it out

set the flags according to your intentions Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and

Intent.FLAG_ACTIVITY_NEW_TASK as follows

 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_NEW_TASK); 
+4
source

Source: https://habr.com/ru/post/1403570/


All Articles