I am trying to open a fragment when I press a notification in the notification bar. My app structure is:
- a base activity with a nav drawer menu
- and some fragment that are opened from the menu
When I press the notification the activity reopens but not in the indicated fragment, and in the LogCat the data from the indicated fragment looks to open and load but not with the UI.
The notification code :
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Comanda Noua");
mBuilder.setContentText("S-a introdus o comanda noua");
mBuilder.setTicker("Comanda noua!");
mBuilder.setSmallIcon(R.drawable.calculator_icon);
mBuilder.setAutoCancel(true);//inchide notificare dupa ce s-a dat click pe ea
//creste numar notificare
mBuilder.setNumber(++numMessages);
// cream un intent
Intent resultIntent = new Intent(this, MainActivity.class);
//am setat actiune pentru a deschide fragmentul corespunzator notificartii la apasare
resultIntent.setAction("Action1");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificationID, mBuilder.build());
The code where I open the fragment when notification is pressed:
Intent intent = getIntent();
try{
String action = intent.getAction();
// Log.d("action:",action);
if(action.equals("Action1")){
//Log.d("deschidem agenda:",action);
AgendaFragment fragment = new AgendaFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(R.id.frame_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null).commit();
}else{
Log.d("eroare", "Intent was null");
}
}catch(Exception e){
Log.e("eroare", "Problem consuming action from intent", e);
}
Why the activity open but the fragment not?