I have an alarm clock app, that is using Alarm Manager and a Broadcast Receiver. The app is one single activity and 4 fragments. When an alarm goes off the onReceive method sends an intent to the main activity, the main activity receives this intent in the method onNewIntent and then moves to the correct fragment. Everything works fine, except when an alarm goes off after the app has been closed.
Once I destroy the app, the alarm still goes off and the intent from the broadcast receiver fires, but the onNewIntent method does catch the intent and move the app to the correct fragment.
Here is the Intent that is in the broadcast receiver class that moves to the main activity
Intent alarmIntent = new Intent( context, ClockActivity.class );
alarmIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmIntent.putExtra("Alarm Name", receivedAlarm.getmName());
context.startActivity(alarmIntent);
Here is my onNewIntent method in my main activity that is not getting called when the alarm is called when the app is closed.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
PhraseFragment phraseFragment = new PhraseFragment();
String activeName = intent.getStringExtra("Alarm Name");
Bundle args = new Bundle();
args.putString("activeName", activeName);
phraseFragment.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.container, phraseFragment)
.addToBackStack("phrase")
.commit();
}