[Apidemos for Android samples video series] app-activity-intentactivityflags

Source: Internet
Author: User

1. Preface

The demo analysis references the mapdigit article about API demos. On the basis of this article, I further analyzed related knowledge points.

2. Demo Effect

3. Demo Analysis

First, enter the intentactivityflags interface, as shown in:

The layout file shows that the layout in this example is simple:

 

<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>  

The entire layout container is a linear layout where each element is arranged vertically and horizontally.

Function:

Click the first "flag_activity_clear_task" button. The program interface will jump to the view-> lists sample interface, as shown in.

Click the second "flag_activity_clear_task (PI)" button, and the program interface will still jump to the views-> lists sample interface.

If you observe more, you will find that when you click either of the two buttons, you will jump to the apidemos-> views-> lists example, then, click "return" to return to apidemos-> views. Then, click "return" to go back to the list interface of apidemos.

Why do the two buttons have the same functions after being clicked? Are there any differences in response event processing after clicking them? Let's first look at a major function code:

private Intent[] buildIntentsToViewsLists() {          // We are going to rebuild our task with a new back stack.  This will          // be done by launching an array of Intents, representing the new          // back stack to be created, with the first entry holding the root          // and requesting to reset the back stack.          Intent[] intents = new Intent[3];             // First: root activity of ApiDemos.          // This is a convenient way to make the proper Intent to launch and          // reset an application's task.          intents[0] = Intent.makeRestartActivityTask(new ComponentName(this,                 com.example.android.apis.ApiDemos.class));             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;  }  

Through the annotations in the function, we can know that the function is used to start an intents array to reconstruct a "return stack". In this example, intent [] has three elements.

Intent [0] is passed

makeRestartActivityTask(new ComponentName(this,                 com.example.android.apis.ApiDemos.class));  

This method returns an intent, which is used to reconstruct an activity task and maintain the status as root. That is to say, after the activity com. example. Android. APIs. apidemos. Class is started, it will serve as the portal activity of this activity task.

Intent [1] and intent [2] both use setclass () to specify the specific intent and send the following keys to their respective keys: "com. example. android. APIS. the "views" and "views/lists" values are stored in the variable of path, which indicates that apidemos obtains the corresponding content and fills in its list.

After reading the main functions, let's take a look at the implementation of the click events of the two buttons:

Click "flag_activity_clear_task" and run the following code: startactivities (buildintentstoviewslists ());

We often use startactivity (intent), while startactivities (intent []) play the same role as startactivities, it is nothing more than adding the jump target activity pointed to by the three intent [] to the current activity stack in sequence from the back to the front, if you use the "back" key after clicking the button, you will find that the returned order is the same as that in intent.

The second button "flag_activity_clear_task (PI)" has the same function as the first button. It only uses pendingintent, which is defined in advance, then start when the conditions are met (even if the current context is destroyed, the external can start this intent from the context in the pendingintent initialization ).

try {                  pi.send();              } catch (CanceledException e) {                  Log.w("IntentActivityFlags", "Failed sending PendingIntent", e);              }          }  

 

4. Video Explanation:Http://www.eyeandroid.com/thread-9802-1-1.html

 

5. We learned this demo:

 

1. Detailed analysis of differences between Android intent and pendingintent

Http://www.eyeandroid.com/thread-1419-1-1.html

2. Duplicate pendingintent. The new pendingintent may not be created when it matches the previous pendingintent.

Http://www.eyeandroid.com/thread-9784-1-1.html

3. Reference: pendingintent Translation

Http://www.eyeandroid.com/thread-9785-1-1.html

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.