Search code examples
androideventsandroid-activitybroadcastreceiverlifecycle

Why activity come back to front via onCreate rather than onRestart?


What I want to do is to start an Activity upon device restarts by sending an intent in a BroadCastReceiver listening BOOT_COMPLETED event. The Activity has a conditional moveTaskToBack in its onCreate event handler. The Activity is launchable and the only Activity of App.

  1. When I reboot the device, App is running and I can tell Activity is hidden from screen. From logcat a 'onCreate' message is print to indicate onCreate event happens. Then I click App icon in screen, expect Activity shows up from back for onCreate shall be omitted and moveTaskToBack shall NOT run. But onCreate event handler is still executed and Activity hides again.
  2. In another way I remove starting Activity from BroadCastReceiver, just open App by click icon in screen. At first tap App starts running and Activity hides, and when I tap icon again, the Activity shows up. From logcat the first event is onRestart, which is expected.

I am not sure what is the different between two ways of bring back Activity? Why onCreate happens twice applying BroadcastReceiver in first case?

Cheers!


Solution

  • I'm now aware that the activity started by broadcast receiver is in a stack, while the activity, which is as same class, started by launcher is in another stack. Thus why onCreate event happened again when I tapped App icon desktop, for App create a new stack and brand-new activity.

    Thanks xvlcw for great help...