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

@@ -341,6 +341,14 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.IntentActivityFlags"
android:label="@string/activity_intent_activity_flags">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<!-- Service Samples --> <!-- Service Samples -->
<service android:name=".app.LocalService" /> <service android:name=".app.LocalService" />
@@ -474,12 +482,16 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.IncomingMessage" android:label="App/Notification/IncomingMessage"> <!-- BEGIN_INCLUDE(no_task_affinity) -->
<activity android:name=".app.IncomingMessage"
android:label="App/Notification/IncomingMessage"
android:taskAffinity="">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter> </intent-filter>
</activity> </activity>
<!-- END_INCLUDE(no_task_affinity) -->
<activity android:name=".app.IncomingMessageView" android:label="App/Notification/IncomingMessageView"> <activity android:name=".app.IncomingMessageView" android:label="App/Notification/IncomingMessageView">
<intent-filter> <intent-filter>

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Demonstrates the user of various intent activity flags.
See corresponding Java code com.android.sdk.app.IntentActivityFlags.java. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
android:text="@string/intent_activity_flags"/>
<Button android:id="@+id/flag_activity_clear_task"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/flag_activity_clear_task">
<requestFocus />
</Button>
<Button android:id="@+id/flag_activity_clear_task_pi"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/flag_activity_clear_task_pi">
<requestFocus />
</Button>
</LinearLayout>

View File

@@ -320,10 +320,19 @@
<!-- app/intents examples strings --> <!-- app/intents examples strings -->
<!-- ============================== --> <!-- ============================== -->
<string name="activity_intents">App/Intents</string> <string name="activity_intents">App/Activity/Intents</string>
<string name="intents">Example of launching various Intents.</string> <string name="intents">Example of launching various Intents.</string>
<string name="get_music">Get Music</string> <string name="get_music">Get Music</string>
<!-- ============================== -->
<!-- app/intents activity flags examples strings -->
<!-- ============================== -->
<string name="activity_intent_activity_flags">App/Activity/Intent Activity Flags</string>
<string name="intent_activity_flags">Example of the use of various intent activity flags.</string>
<string name="flag_activity_clear_task">FLAG_ACTIVITY_CLEAR_TASK</string>
<string name="flag_activity_clear_task_pi">FLAG_ACTIVITY_CLEAR_TASK (PI)</string>
<!-- =================================== --> <!-- =================================== -->
<!-- app/notification examples strings --> <!-- app/notification examples strings -->
<!-- =================================== --> <!-- =================================== -->

View File

@@ -65,8 +65,10 @@ public class IncomingMessage extends Activity {
CharSequence message = "kthx. meet u for dinner. cul8r"; CharSequence message = "kthx. meet u for dinner. cul8r";
// The PendingIntent to launch our activity if the user selects this notification // The PendingIntent to launch our activity if the user selects this notification
//BEGIN_INCLUDE(pending_intent)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, IncomingMessageView.class), 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 // The ticker text, this uses a formatted string so our message could be localized
String tickerText = getString(R.string.imcoming_message_ticker_text, message); 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 // The PendingIntent to launch our activity if the user selects this
// notification. Note the use of FLAG_UPDATE_CURRENT so that if there // notification. Note the use of FLAG_UPDATE_CURRENT so that if there
// is already an active matching pending intent, we will update its // 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, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, NotificationDisplay.class) new Intent(this, NotificationDisplay.class).putExtra("moodimg", moodId),
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("moodimg", moodId),
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
return contentIntent; 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) { 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 // In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(textId); 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 method sets the defaults on the notification before posting it.
// This is who should be launched if the user selects our notification. // This is who should be launched if the user selects our notification.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, PendingIntent contentIntent = makeDefaultIntent();
new Intent(this, StatusBarNotifications.class), 0);
// In this sample, we'll use the same text for the ticker and the expanded notification // 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); CharSequence text = getText(R.string.status_bar_notifications_happy_message);