1 package com.example.android.apis.app;
2 
3 import com.example.android.apis.R;
4 
5 import android.app.Activity;
6 import android.app.PendingIntent;
7 import android.app.PendingIntent.CanceledException;
8 import android.content.ComponentName;
9 import android.content.Context;
10 import android.content.Intent;
11 import android.os.Bundle;
12 import android.util.Log;
13 import android.view.View;
14 import android.view.View.OnClickListener;
15 import android.widget.Button;
16 
17 /**
18  * Example of various Intent flags to modify the activity stack.
19  */
20 public class IntentActivityFlags extends Activity {
21     @Override
onCreate(Bundle savedInstanceState)22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24 
25         setContentView(R.layout.intent_activity_flags);
26 
27         // Watch for button clicks.
28         Button button = (Button)findViewById(R.id.flag_activity_clear_task);
29         button.setOnClickListener(mFlagActivityClearTaskListener);
30         button = (Button)findViewById(R.id.flag_activity_clear_task_pi);
31         button.setOnClickListener(mFlagActivityClearTaskPIListener);
32     }
33 
34     /**
35      * This creates an array of Intent objects representing the back stack
36      * for a user going into the Views/Lists API demos.
37      */
38 //BEGIN_INCLUDE(intent_array)
buildIntentsToViewsLists()39     private Intent[] buildIntentsToViewsLists() {
40         // We are going to rebuild our task with a new back stack.  This will
41         // be done by launching an array of Intents, representing the new
42         // back stack to be created, with the first entry holding the root
43         // and requesting to reset the back stack.
44         Intent[] intents = new Intent[3];
45 
46         // First: root activity of ApiDemos.
47         // This is a convenient way to make the proper Intent to launch and
48         // reset an application's task.
49         intents[0] = Intent.makeRestartActivityTask(new ComponentName(this,
50                 com.example.android.apis.ApiDemos.class));
51 
52         Intent intent = new Intent(Intent.ACTION_MAIN);
53         intent.setClass(IntentActivityFlags.this, com.example.android.apis.ApiDemos.class);
54         intent.putExtra("com.example.android.apis.Path", "Views");
55         intents[1] = intent;
56 
57         intent = new Intent(Intent.ACTION_MAIN);
58         intent.setClass(IntentActivityFlags.this, com.example.android.apis.ApiDemos.class);
59         intent.putExtra("com.example.android.apis.Path", "Views/Lists");
60 
61         intents[2] = intent;
62         return intents;
63     }
64 //END_INCLUDE(intent_array)
65 
66     private OnClickListener mFlagActivityClearTaskListener = new OnClickListener() {
67         public void onClick(View v) {
68             startActivities(buildIntentsToViewsLists());
69         }
70     };
71 
72     private OnClickListener mFlagActivityClearTaskPIListener = new OnClickListener() {
73         public void onClick(View v) {
74             Context context = IntentActivityFlags.this;
75 //BEGIN_INCLUDE(pending_intent)
76             PendingIntent pi = PendingIntent.getActivities(context, 0,
77                     buildIntentsToViewsLists(), PendingIntent.FLAG_UPDATE_CURRENT);
78 //END_INCLUDE(pending_intent)
79             try {
80                 pi.send();
81             } catch (CanceledException e) {
82                 Log.w("IntentActivityFlags", "Failed sending PendingIntent", e);
83             }
84         }
85     };
86 }
87