I have same problem as many but doesn't seems to find solution by their answers, so I have to write my own..
I have a notification from which I would like to open a fragment by clicking on it but it doesn't open WHY?
here is my code
public void showStatusNotification() {
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction("OpenStatusFragment");
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.dc_feedback)
.setAutoCancel(true)
.setContentTitle("Order Id DC256649")
.setContentIntent(resultPendingIntent)
.setContentText("Your Order has been cancelled as per your request");
//mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId2, mBuilder.build());
Log.v("notification","Notification");
}
I am defining this code in MainActivity.java only
In onCreate
method I am opening fragment like
final android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
if(getIntent().getAction().equals("OpenNotificationFragment")) {
fragmentManager.beginTransaction()
.replace(R.id.fragment_main_container, new NotificationFragment()).addToBackStack(PastOrderFragment.TAG)
.commitAllowingStateLoss();
mDrawerLayout.closeDrawer(mDrawerList);
}
else {
fragmentManager.beginTransaction()
.replace(R.id.fragment_main_container, new OrderStatusFragment()).addToBackStack(PastOrderFragment.TAG)
.commitAllowingStateLoss();
mDrawerLayout.closeDrawer(mDrawerList);
}