0

I have an application in which I am calling ACTION_SEND intent from my main activity. Everything works fine but when I return to my main activity from ACTION_SEND intent then main activity starts its execution from onCreate() method but I want it to be started from onRestart() method. I think its bcz a OS kills my activity to free up space. So is there any way rather than using Service So that I can stop OS from killing my activity ?

Vikalp
  • 2,051
  • 4
  • 18
  • 24

1 Answers1

0

I think its bcz a OS kills my activity to free up space

Possibly. You might be doing something yourself to destroy your activity (e.g., calling finish() after calling startActivity() for your ACTION_SEND Intent).

So is there any way rather than using Service So that I can stop OS from killing my activity ?

No. Even a service may not help. Your process can be terminated at any time, for any reason, including the user deciding to terminate it via a task manager or the recent-tasks list. Also, if the user rotates the screen or causes another configuration change while in whatever app they started via ACTION_SEND, your activity that called startActivity() with ACTION_SEND will be recreated due to the configuration change.

You cannot assume that onRestart() will be called. In general, I consider onRestart() to be a code smell for just this reason.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have checked my code and I am not calling finish() after startActivity(). And one more thing is that onDestroy() method of main activity never gets called but when I press back button from ACTION_SEND Intent(like Bluetooth activity), it automatically starts main activity from onCreate(). What I am not getting is that how can activity starts from onCreate() without calling onDestroy(). Please put some light on it. – Vikalp Apr 20 '14 at 15:23
  • @Vikalp: "but when I press back button from ACTION_SEND Intent(like Bluetooth activity), it automatically starts main activity from onCreate()" -- that means your process was terminated. Activities do not have to be called with `onDestroy()`. Only ones that are finished, via BACK or you calling `finish()` or similar means, will be destroyed and go through `onDestroy()`. Any running activities in your process when the process is terminated are not destroyed. – CommonsWare Apr 20 '14 at 15:36
  • Ok I got it but Isn't there any way that I can resume state of my main activity to where I left of ? – Vikalp Apr 20 '14 at 15:42
  • @Vikalp: That depends on what the "state" is. `onSaveInstanceState()` should be called on your activity around the time the `ACTION_SEND` activity comes to the foreground. You are welcome to put stuff in that `Bundle`. You should get that `Bundle` back in `onCreate()` and `onRestoreInstanceState()` of your new activity instance, since the user is effectively navigating back to a specific spot. This is standard activity lifecycle stuff, see https://developer.android.com/guide/components/activities.html#SavingActivityState – CommonsWare Apr 20 '14 at 15:50
  • I have used a Service Class to do my work and that solved my problem. I bound my main activity to service class and now OS isn't killing my activity (OS will only kill my activity under extreme memory requirement) – Vikalp Apr 21 '14 at 05:44