Android Hundred Day Program: Using intent Callback results

Source: Internet
Author: User

This chapter uses intent to return the desired results;

Process:

Start the home page, press the button to start the second activity-> enter the user name, press the OK button, return the user name back to the home page, read the user name and display it.

1 Start the home page map:


2 Start a second activity


3 Enter user name:


4 Upload to Home page, read and display:


Create a new project, and then enter the corresponding code to be OK.

1 home Page Interface Code (ACTIVITY_MAIN.XML):

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= "com.example.intenttest.MainActivity" >    <button        android:layout_width= "Wrap_ Content "        android:layout_height=" wrap_content "        android:text=" @string/activate_secondary "        android:o nclick= "OnClick"/></linearlayout>

Create a button and specify that the corresponding time is the onclick

2 Response time Code for home page:

public class Mainactivity extends activity {/* * An integer value of identifies an activity is calling. * When A Ctivity returns a value, you must has a-a-identify it * If The request code is set to-1 and then calling it using the< c0/>* Startactivityforresult () method is equivalent to calling it using * the  startactivity () method. That is, no result would be returned. */int Requestcode = 1;public static<t> boolean equ (t T1, T T2) {return T1 = = t2;}    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    }        public void OnClick (view view) {    Startactivityforresult (new Intent ("Su.bill.intenttest.SecondaryActivity"), Requestcode);}    }

Here and the general startactivity does not need is uses the Startactivityforresult, uses this function to be able to return the result.

3 Enter the interface code for the second activity:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Horizontal" >    <edittext        android:id= "@+id/txt_username"        android:layout_weight= "1"        android:layout_width= "0DP"        android:layout_height= "wrap_content"                android:hint= "@string/your_name"/ >    <button        android:id= "@+id/btn_ok"        android:layout_width= "wrap_content"        android:layout _height= "Wrap_content"        android:onclick= "OnClick"        android:text= "@string/ok_button"/></ Linearlayout>

Simple one input text, one key

4 logical code for the second activity

Package Com.example.intenttest;import Android.app.activity;import Android.content.intent;import Android.net.Uri; Import Android.os.bundle;import Android.view.view;import Android.widget.edittext;public class SecondaryActivity Extends Activity {@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.secondary_activity);} public void OnClick (View v) {Intent data = new Intent (); EditText txt_username = (EditText) Findviewbyid (r.id.txt_username);d ata.setdata (Uri.parse (Txt_username.gettext (). ToString ())); Setresult (RESULT_OK, data); Finish ();}}

The main is the onclick code, create a new intent data, set up the content of this intent, and then use Setresult return this intent to the homepage activity, in fact, the basic principle or use intent communication.


5 home Page logic reads and displays the result of the code:

    @Override public    void Onactivityresult (int reqcode, int rescode, Intent data) {    if (equ (Reqcode, Requestcode) &am p;& equ (Rescode, RESULT_OK)) {    Toast.maketext (this, data.getdatastring (), Toast.length_short). Show ();    }    }
The function is called automatically from the last corresponding activity return, where the representation symbol is REQUESTCODE and the returned identifier Reqcode corresponds to confirm that the returned intent is the corresponding activity returned.

The complete code for the home page logic is as follows:

Package Com.example.intenttest;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.menu;import android.view.menuitem;import Android.view.view;import android.widget.Toast;public  Class Mainactivity extends Activity {/* * An integer value of identifies an activity is calling. * When an activity Returns a value, you must has a-a-identify it * If The request code is set to-1 and then calling it using the * STAR Tactivityforresult () method is equivalent to calling it using * the StartActivity () method. That is, no result would be returned.    */int Requestcode = 1;public static<t> boolean equ (t T1, T T2) {return T1 = = t2;}        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } public void OnClick (view view) {Startactivityforresult (New Intent ("Su.bill.intenttest.SecondaryActivity"), req    Uestcode); } @OveRride public void Onactivityresult (int reqcode, int rescode, Intent data) {if (Equ (Reqcode, Requestcode) &&    Equ (Rescode, RESULT_OK)) {Toast.maketext (this, data.getdatastring (), Toast.length_short). Show (); }} @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the        Action Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}

Other code that is not mentioned is the code that is automatically generated when you create a new project, and you can ignore it temporarily.


is to be familiar with some of the mechanisms of Android, it is very difficult to understand.


It is a hundred-day program, it seems can not be completed within hundred days, because there are many other things to do.

Strive to do faster, after a few boutique tutorials, to learn, so that more people learn is also a very meaningful thing.


Android Hundred Day Program: Using intent Callback results

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.