EGHDK
EGHDK

Reputation: 18130

Android - Click on notification opens up my main activity, how do I make it open up something else

I have a simple app that shows android notifications depending to one of three buttons you press.

Dog, Cat, or Mouse

If you press dog then a notification that says "Dog" shows. If you press cat then a notification that says "Cat" shows. If you press mouse then a notification that says "Mouse" shows.

When you click the notification it just takes me to my main activity (with the three buttons again). I would like it to take me to a separate activity, that will read what notification was clicked, and set it the text from the notification as a textView.

Any ideas on how to do this?

Upvotes: 0

Views: 1491

Answers (1)

speakingcode
speakingcode

Reputation: 1506

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.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);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());

Source: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Actions

Upvotes: 2

Related Questions