0

Can I call recreate() in onDestroy() method of MainActivity? The idea is to restart the activity whenever OS stops it. Is it possible and are there any disadvantages of doing this. Basically, I have a blank activity that starts many services, but the app stops unexpectedly (due to which all services too stops) on one of the manufacturer (while it works fine for most of the phones). For instance something like this is possible?

public void onDestroy()
      {
          super.onDestroy();
          this.recreate();
      }

UPDATE 1 Restart the main activity after 2 hours, any suggestion to this approach. Is it bad, since main activity will call itself again.

Intent ishintentC = new Intent(this, MainActivity.class);
PendingIntent pintentC = PendingIntent.getService(this, 0, ishintentC, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmC = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmC.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+7200000,AlarmManager.INTERVAL_HOUR*2, pintentC);
And_Dev
  • 113
  • 2
  • 12

1 Answers1

0

onDestroy() for an Activity isn't guaranteed to be called.

If the OS decides to kill your activity, it won't call onDestroy(), so that plan won't work.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
  • Ok, thanks, is there any other option? Alternatively, I was planning to do an alarmManager that recreates after say x hours, any thoughts? – And_Dev Oct 23 '15 at 01:36
  • You can take a look at this: http://stackoverflow.com/questions/11625973/restarting-android-application-after-process-is-killed but if your app crashes, i don't know if alarms survive, though I would think not and not sure what can be done there. – CmosBattery Oct 23 '15 at 01:49
  • Thanks @CmosBattery, app is stopped by OS, not crashing. I am hoping try something like my update 1. Any comments? – And_Dev Oct 23 '15 at 02:28