Reputation: 2783
I am using latest Lollipop style Navigation drawer in my app.Please refer to this example for more info on that.I use Fragments to show different navigation tabs. Now, I need to open, let's say the 5th item in drawer when I click a certain notification from the notification bar in android device. I am stuck in as to how to switch directly to that Fragment by clicking notification. I am very much aware as how this can be done using Activity. Can anyone please suggest me any solution regarding this ?
Thanks in Advance.
Resolved:
I have resolved this issue by following Ziem's answer. I have just added following lines to open it as a new screen and clear older activity stack:
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
Upvotes: 0
Views: 146
Reputation: 6697
You can add PendingIntent
to notification's click:
PendingIntent resultPendingIntent;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
...
.setContentIntent(resultPendingIntent);
Next you need to handle notification's Intent
inside your activity.
Example:
// How to create notification with Intent:
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("open", 1);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(resultPendingIntent);
int mNotificationId = 33;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
//How to handle notification's Intent:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getIntent() != null && getIntent().hasExtra("open")) {
int fragmentIndexToOpen = getIntent().getIntExtra("open", -1)
// show your fragment
}
}
}
Upvotes: 1