I'm writing simple alarm clock. It contains MainActivity with a ListView, WakeActivity with wake up message and a PlayerService that plays sound. I use the following code to set alarm clock:
Intent i = new Intent(context, WakeupActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, a.getId(), i, 0);
Calendar cal = getNextAlarm();
if(cal!=null){
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
if(toast) showToast(cal);
}
In android manifest WakeupActivity is defined as
android:launchMode="singleTask"
In onCreate method I init close button. In onResume I start the PlayerService
Intent i = new Intent(this, PlayerService.class.); startActivity(i);
When user click close button I call fnish();
and, at last, in onPause() I stop PlayerService. It works but there is one case I can't understand.
- I set alarm and leave main activity on the top. WakeupActivity and PlayerService starts successfully. When I press close button I get back to MainActivity and everything's ok.
- I set alarm and press home button. WakeupActivity and PlayerService starts successfully. When I press close button then music stops and onDestroy() for WakeupActivity calls (like in 1 case)... Seems good but when I hold home button and choose my application then I get back to WakeupActivity and the music starts again!!! What the hell? I finish that activity! Why so? Please, help!