0

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!

Alan Hoo
  • 445
  • 3
  • 12
  • Add a log statement to onDestroy() and see if it is called. If so, onCreate() must be called again. Also, some code would be helpful. – Tobias Uhmann Mar 14 '18 at 13:17
  • No onDestroy() is called as I checked using logcat. – Alan Hoo Mar 14 '18 at 13:18
  • As shown in this graphic (https://developer.android.com/guide/components/activities/activity-lifecycle.html) onCreate will also be called if your app gets killed by the system. Might your app be killed for some reason? – Tobias Uhmann Mar 14 '18 at 13:20
  • 1
    You might want to check the activity stack. Maybe you've got multiple instances of your activity on it (each of which was created by `onCreate()`) as described here: https://stackoverflow.com/questions/2442713/view-the-tasks-activity-stack/43643933 – Tobias Uhmann Mar 14 '18 at 13:28
  • That's the weird thing. I didn't see onDestroy(). System resource is sufficient for this simple App. The App has this only one Activity. What confuses me is the way first time Activity is hidden. If I start the Activity from within a BroadCastReceiver using sending Intent, I will re-run onCreate again when I tap App icon. But If I start the Activity by tap App Icon first, then tap again, the onCreate won't be running again. – Alan Hoo Mar 14 '18 at 13:33

1 Answers1

0

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...

Alan Hoo
  • 445
  • 3
  • 12