Bring application back to front

前端 未结 3 1860
轮回少年
轮回少年 2021-02-19 14:18

My application is a timer with alarm type app. When the timer expires i would like to bring the app\'s main activity back into view, if the user has navigated away, but the app

相关标签:
3条回答
  • 2021-02-19 15:05

    For me it was this:

    Intent intent = new Intent(Application.getContext(), AbstractFragmentActivity.instance.getClass());
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Application.getContext().startActivity(intent);
    
    0 讨论(0)
  • 2021-02-19 15:10

    See http://developer.android.com/guide/appendix/faq/framework.html#4:

    How can I check if an Activity is already running before starting it?

    The general mechanism to start a new activity if its not running— or to bring the activity stack to the front if is already running in the background— is the to use the NEW_TASK_LAUNCH flag in the startActivity() call.

    0 讨论(0)
  • 2021-02-19 15:19

    You should put your timer in a Service, that way you're assured that Android won't kill your application while it's in background. (You can put your whole MyCount class in a service).

    From the service you can always bring your application (Activity) to the front:

    Intent intent = new Intent(getBaseContext(), TimerActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
    startActivity(intent);
    
    0 讨论(0)
提交回复
热议问题