Sample code for new APIs to support new back stack / task navigation

Change-Id: I79d1933b894b049711781c39b8e15dbce1626448
This commit is contained in:
Dianne Hackborn
2010-11-22 16:15:32 -08:00
parent 24ba0688a6
commit 41430b9f30
6 changed files with 200 additions and 8 deletions

View File

@@ -65,8 +65,10 @@ public class IncomingMessage extends Activity {
CharSequence message = "kthx. meet u for dinner. cul8r";
// The PendingIntent to launch our activity if the user selects this notification
//BEGIN_INCLUDE(pending_intent)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, IncomingMessageView.class), 0);
//END_INCLUDE(pending_intent)
// The ticker text, this uses a formatted string so our message could be localized
String tickerText = getString(R.string.imcoming_message_ticker_text, message);

View File

@@ -0,0 +1,90 @@
package com.example.android.apis.app;
import com.example.android.apis.R;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* Example of various Intent flags to modify the activity stack.
*/
public class IntentActivityFlags extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent_activity_flags);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.flag_activity_clear_task);
button.setOnClickListener(mFlagActivityClearTaskListener);
button = (Button)findViewById(R.id.flag_activity_clear_task_pi);
button.setOnClickListener(mFlagActivityClearTaskPIListener);
}
/**
* This creates an array of Intent objects representing the back stack
* for a user going into the Views/Lists API demos.
*/
//BEGIN_INCLUDE(intent_array)
private Intent[] buildIntentsToViewsLists() {
// We will use FLAG_ACTIVITY_CLEAR_TASK to complete replace our
// current task with a new Intent.
Intent[] intents = new Intent[3];
// The main activity started from launcher is action MAIN and
// category LAUNCHER; we want to match that.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// We will use FLAG_ACTIVITY_CLEAR_TASK to completely replace our
// current task with a new Intent.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Set the actual activity to launch.
intent.setClass(IntentActivityFlags.this, com.example.android.apis.ApiDemos.class);
intents[0] = intent;
intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(IntentActivityFlags.this, com.example.android.apis.ApiDemos.class);
intent.putExtra("com.example.android.apis.Path", "Views");
intents[1] = intent;
intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(IntentActivityFlags.this, com.example.android.apis.ApiDemos.class);
intent.putExtra("com.example.android.apis.Path", "Views/Lists");
intents[2] = intent;
return intents;
}
//END_INCLUDE(intent_array)
private OnClickListener mFlagActivityClearTaskListener = new OnClickListener() {
public void onClick(View v) {
startActivities(buildIntentsToViewsLists());
}
};
private OnClickListener mFlagActivityClearTaskPIListener = new OnClickListener() {
public void onClick(View v) {
Context context = IntentActivityFlags.this;
//BEGIN_INCLUDE(pending_intent)
PendingIntent pi = PendingIntent.getActivities(context, 0,
buildIntentsToViewsLists(), PendingIntent.FLAG_UPDATE_CURRENT);
//END_INCLUDE(pending_intent)
try {
pi.send();
} catch (CanceledException e) {
Log.w("IntentActivityFlags", "Failed sending PendingIntent", e);
}
}
};
}

View File

@@ -148,15 +148,52 @@ public class StatusBarNotifications extends Activity {
// The PendingIntent to launch our activity if the user selects this
// notification. Note the use of FLAG_UPDATE_CURRENT so that if there
// is already an active matching pending intent, we will update its
// extras to be the ones passed in here.
// extras (and other Intents in the array) to be the ones passed in here.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, NotificationDisplay.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("moodimg", moodId),
new Intent(this, NotificationDisplay.class).putExtra("moodimg", moodId),
PendingIntent.FLAG_UPDATE_CURRENT);
return contentIntent;
}
//BEGIN_INCLUDE(intent_array)
private PendingIntent makeDefaultIntent() {
// A typical convention for notifications is to launch the user deeply
// into an application representing the data in the notification; to
// accomplish this, we can build an array of intents to insert the back
// stack stack history above the item being displayed.
Intent[] intents = new Intent[4];
// First: root activity of ApiDemos.
// The main activity started from launcher is action MAIN and
// category LAUNCHER; we want to match that.
intents[0] = new Intent(Intent.ACTION_MAIN);
intents[0].addCategory(Intent.CATEGORY_LAUNCHER);
// We will use FLAG_ACTIVITY_CLEAR_TASK to completely replace our
// current task with a new Intent.
intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
intents[0].setClass(this, com.example.android.apis.ApiDemos.class);
// "App"
intents[1] = new Intent(this, com.example.android.apis.ApiDemos.class);
intents[1].putExtra("com.example.android.apis.Path", "App");
// "App/Notification"
intents[2] = new Intent(this, com.example.android.apis.ApiDemos.class);
intents[2].putExtra("com.example.android.apis.Path", "App/Notification");
// Now the activity to display to the user.
intents[3] = new Intent(this, StatusBarNotifications.class);
// The PendingIntent to launch our activity if the user selects this
// notification. Note the use of FLAG_UPDATE_CURRENT so that if there
// is already an active matching pending intent, we will update its
// extras (and other Intents in the array) to be the ones passed in here.
PendingIntent contentIntent = PendingIntent.getActivities(this, 0,
intents, PendingIntent.FLAG_UPDATE_CURRENT);
return contentIntent;
}
//END_INCLUDE(intent_array)
private void setMood(int moodId, int textId, boolean showTicker) {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(textId);
@@ -210,8 +247,7 @@ public class StatusBarNotifications extends Activity {
// This method sets the defaults on the notification before posting it.
// This is who should be launched if the user selects our notification.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, StatusBarNotifications.class), 0);
PendingIntent contentIntent = makeDefaultIntent();
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.status_bar_notifications_happy_message);