Android: How to Resume Application/Activity from BroadcastReceiver?

半腔热情 提交于 2020-01-14 13:04:32

问题


If my procedure is following:

  1. Launch Activity A -> Activity B
  2. Press 'Home' button.
  3. Click on the application again.

Result: 'Activity B' shows up (it resumes).

  1. Launch Activity A -> Activity B
  2. Press 'Back' button.
  3. Click on the application again.

Result: 'Activity A' shows up (it restarts).

I want to do exactly same from the BroadcastReceiver.

  1. Launch Activity A -> Activity B
  2. Press 'Home' button.
  3. BroadcastReceiver receives a broadcast and want to "resume" application.

My expected result: 'Activity B' shows up.

I want to do exactly same from the BroadcastReceiver.

  1. Launch Activity A -> Activity B
  2. Press 'Back' button.
  3. BroadcastReceiver receives a broadcast and want to "restart" application.

Current result: 'Activity A' shows up.

Following code doesn't do 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.


回答1:


My gosh, I made it working!!

Thank you for other answers you guys provided, but they weren't what I was looking for.

This will do the job:

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



回答2:


check out this

set flags to your intent Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and

Intent.FLAG_ACTIVITY_NEW_TASK as following

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_NEW_TASK);


来源:https://stackoverflow.com/questions/9881976/android-how-to-resume-application-activity-from-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!