0

I issue the a notification with an action that opens mainactivity. This is the first time the activity is called, created and everything works perfectly. After I pause the activity I automatically call onDestroy() of the activity and it is destroyed with success. When I click the notification for the second time, however, there is an error that says FATAL EXCEPTION: main can't resume activity. If the activity was destroyed I wasn't expecting it to be resumed but recreated again... why is this happening and how can I force the recreation of the activity by these methods?

Fane
  • 1,978
  • 8
  • 30
  • 58
  • http://stackoverflow.com/questions/4553605/difference-between-onstart-and-onresume – Hayden Feb 15 '15 at 23:29
  • @Hayden I read it, still I don't understand how can I force a total recreate (oncreate) of the activity – Fane Feb 15 '15 at 23:33

1 Answers1

0

Do not call onDestroy() directly!

These are lifecycle methods that the system should call. The problem you are having is that the system still believes the activity is paused (rather than destroyed as you would have liked) since it was never notified. You undoubtedly invoked super.onDestroy() within your onDestroy() method (the right thing) which caused part of the activity to clean itself up, making it non-resumable. When the system comes around the resume the activity in which it thought was paused, it throws that error.

Fortunately, there are ways to tell the system to kill your activity when you want it, through a method called finish().

Android documentation:

Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

Replace your call to onDestroy() with this call should resolve your issue.

Never call lifecycle methods directly!

initramfs
  • 8,275
  • 2
  • 36
  • 58