As the title says, usually onDestroy() is only called if the phone needs to free up memory.
My flow: Activity (A) sets a recurring Alarm to send a notification to the user in A's onCreate method:
public void startEMAAlarm(){
Log.d(TAG, "startEMAAlarm: in start ema alarm");
Calendar cal = Calendar.getInstance();
long when = cal.getTimeInMillis();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 30);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, EMAAlarmReceiver.class);
startEMAIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000 * 60 * 20, startEMAIntent);
Log.d(TAG, "startEMAAlarm: alarm shjould be set");
alarmStarted = true;
}
The alarm receiver gets this, sends a notification, and if the notification is selected, it starts activity B:
EMAAlarmReceiver: OnReceive{
Intent resultIntent = new Intent(context, EMA.class);
//resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.cdmh_small, "SNOOZE", snoozePendingIntent).build();
Notification mBuilder =
new NotificationCompat.Builder(context, CHANNEL_DI)
.setSmallIcon(R.drawable.noti_icon)
.setContentTitle("EMA")
.setAutoCancel(true)
.setContentText("Time for another EMA :)")
.setOngoing(true)
.setChannelId(CHANNEL_DI)
.setSound(uri)
.setContentIntent(pendingIntent)
.addAction(action)
.build();
mNotificationManager.notify("first",1, mBuilder);
Log.d(TAG, "onReceive OREO: should be notification built now");
The notification then appears on the phone, but as soon as I click it, Activity A has its onDestroy() method called, and I am not sure why.
It does not really matter, because i can use savedInstance etc to recreate it later ( ie Activity B returns to Activity A via another Intent when leaving), but I am not sure why it is being destroyed at that point. As you can see I am not currently using any flags, but I have experimented with lots of them, and it does not seem to change the behavior.
Not sure if it matters, but Activity B is just a collection of SeekBars the user slides over, it then saves their value to a text file, and returns to Activity A
How Activity A looks in Manifest:
<activity
android:name="com.anysoftkeyboard.ui.settings.setup.FinishInstallScreen"
android:screenOrientation="portrait">
How activity B looks:
<activity android:name="com.radicalninja.logger.EMA"
android:screenOrientation="portrait"/>
Ok, for anyone else that may randomly come across this problem, it was caused by my own stupidity.
Basically I had made some changes to my application, and had forgot to update the TaskStackBuilder part of my notification, so I was building the Parent Stack incorrectly.
This caused the calling activities onDestroy() method to be called everytime.