android intent - onNewIntent not called on restart -


i have alarm clock app, using alarm manager , broadcast receiver. app 1 single activity , 4 fragments. when alarm goes off onreceive method sends intent main activity, main activity receives intent in method onnewintent , moves correct fragment. works fine, except when alarm goes off after app has been closed.

once destroy app, alarm still goes off , intent broadcast receiver fires, onnewintent method catch intent , move app correct fragment.

here intent in broadcast receiver class moves 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 onnewintent method in main activity not getting called when alarm called when app 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();  } 

its bit late maybe can someone.

as see onnewintent called when activity opened on background. when send intent activity isn't running on background can retrieve through getintent on onresume().

i change code following.

@override protected void onresume() {     super.onresume();      intent intent = getintent();     string activename = intent.getstringextra("alarm name");      if (activename != null){         phrasefragment phrasefragment = new phrasefragment();          bundle args = new bundle();         args.putstring("activename", activename);         phrasefragment.setarguments(args);          getfragmentmanager().begintransaction()                 .replace(r.id.container, phrasefragment)                .addtobackstack("phrase")                .commit();      } }  @override protected void onnewintent(intent intent) {     super.onnewintent(intent);     setintent(intent); } 

in case need check if intent you've receive in onresume() contains data require.

note haven't found reference in documentation. conclusion got experimenting.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -