criver.to
criver.to

Reputation: 540

How to have a notification open a view within an activity?

Right now I have an alarm on my main activity which launches a class which launches a notification on the status bar. When I click on the notification, it opens up my main activity, now I want it to open a n specific view within that activity.

This is my notification code.

       String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
    int icon = android.R.drawable.stat_notify_chat;        // icon from resources
    CharSequence tickerText = "TickerText";              // ticker-text
    long when = System.currentTimeMillis();         // notification time
    CharSequence contentTitle = "My notification";  // message title
    CharSequence contentText = "Hello World!";      // message text

    //This is the intent to open my main activity when the notification is clicked
    final Intent notificationIntent = new Intent(context, mainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    // the next two lines initialize the Notification, using the configurations above
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    mNotificationManager.notify(notifID, notification);

This is the button in the layout used by my main activity which opens the view I want the notification to open (graph view).

    <Button
    android:id="@+id/graphsButton"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_below="@id/reviewButton"
    android:layout_toRightOf="@id/medicineButton"
    android:onClick="openGraphs"
    android:text="Graphs" />

When you click on the button. It executes the following method in the main class.

   public void openGraphs(View v) {
    canExit = false;
    setContentView(R.layout.graphs);
}

So basically, I got the notification to open the app and launch the main activity, but I want it to launch the graph view directly.

Can anybody help me?

Upvotes: 0

Views: 403

Answers (2)

Sudhaker
Sudhaker

Reputation: 785

Is there anything stopping you from showing the graph directly in the Activity?

In the above Activity instead of having a button, to show the Graph when clicked, directly set the view to R.layout.graphs in onCreate() method.

In case you have the prescribed activity for some other purpose then create a separate activity just to show the graph and point to it from the notificationIntent.

Upvotes: 0

Stefan
Stefan

Reputation: 4705

You could add a flag to the pending intent set in the notification using the intent's extras. Evaluate the intent when the activity is started. If you find the flag in the starting intent execute the the code in openGraphs(). Make sure to get the most recent intent (not the one which may have started the activity earlier, here is some advice on that: https://stackoverflow.com/a/6838082/1127492).

Upvotes: 5

Related Questions