Activity instances to start the activity and return the results _android

Source: Internet
Author: User
Tags gettext lenovo

First to show you the results of the display chart:

1 Introduction

If you want to get the data returned after the new open activity closes in the activity, you need to open the new activity using the system-supplied Startactivityforresult (Intent Intent, int requestcode) method. After the new activity is closed, the data is returned to the previous activity, and in order to get the returned data, the Onactivityresult (int requestcode, int resultcode, Intent) must be rewritten in the preceding activity. Data) method.

 public class Mainactivity extends activity {Private final static String tag= "Mainactivi"
Ty "; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.main);
Button btnopen= (button) This.findviewbyid (R.id.btnopen); Btnopen.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {//data returned after the newly opened activity is closed//
The second parameter is the request code, which can be numbered according to the business requirement Startactivityforresult (new Intent (Mainactivity.this, Otheractivity.class), 1);
}
}); /** * To get the data returned, the Onactivityresult method * * Requestcode request code must be overridden in the preceding activity (referring to the Mainactivity Class), that is, call Startactivityforresult () Passes past values * ResultCode result codes, which are used to identify which new activity the returned data came from/@Override protected void Onactivityresult (int requestcode, int result Code, Intent data) {String result = Data.getextras (). getString ("resulting"), and/or get returned after the new activity is closed log.i (TAG, result);} 

When the new activity is closed, the data returned by the new activity is passed through intent, and the Android platform invokes the Onactivityresult () method of the preceding activity, The intent that holds the returned data is passed as a third input parameter, and the third input parameter in the Onactivityresult () method can be used to fetch the data returned by the new activity.

2 Setresult

Open a new activity using the Startactivityforresult (Intent Intent, int requestcode) method, The need to return data to the previous activity before the new activity closes needs to be implemented using the system-supplied setresult (int resultcode, Intent data) method:

public class Otheractivity extends activity {
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.other);
Button btnclose= (button) Findviewbyid (r.id.btnclose);
Btnclose.setonclicklistener (New View.onclicklistener () {public
void OnClick (View v) {
//data is returned
using intent Intent Intent = new Intent ();
Deposit the return Data
in intent Intent.putextra ("result", "My Name is Linjiqin");
Sets the return data
OtherActivity.this.setResult (RESULT_OK, intent);
Close activity
OtherActivity.this.finish ();}}
);
}

The first parameter value of the Setresult () method can be defined according to the business needs, and the RESULT_OK used in the code above is a constant defined by the system activity class, with a value of-1, and the code snippet is as follows:

public class Android.app.Activity extends ... {public
static final int result_canceled = 0;
public static final int result_ok =-1;
public static final int result_first_user = 1;
}

Note: When you click on the "Open New Activity" button, you will jump to the "I am the newly opened Activity" page;

When you click the "Close" button, close the current page and jump to the "I am old Activity" page and pass the result parameter to the previous activity

3 function of the request code

To open a new activity using the Startactivityforresult (Intent Intent, int requestcode) method, we need to pass in a request code for the Startactivityforresult () method ( Second parameter). The value of the request code is set by itself based on the business needs to identify the source of the request. For example: An activity has two buttons, and clicking both buttons opens the same activity, whether it's the button that opens the new activity, and when the new activity closes, the system invokes the onactivityresult of the preceding activity. (int requestcode, int resultcode, Intent data) method. In the Onactivityresult () method, you can do this if you need to know that the new activity is opened by that button and that you want to make the appropriate business process:

@Override public void OnCreate (Bundle savedinstancestate) {
....
Button1.setonclicklistener (New View.onclicklistener () {public
void OnClick (View v) {
Startactivityforresult (New Intent (Mainactivity.this, Newactivity.class), 1);
}
);
Button2.setonclicklistener (New View.onclicklistener () {public
void OnClick (View v) {
Startactivityforresult (New Intent (Mainactivity.this, Newactivity.class), 2);
}
);
@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
switch (requestcode) { Case
1:
//From button 1 request, for the corresponding business processing case
2:
//From button 2 request, for the corresponding business processing
}
}

4 effect of the result code

In an activity, it is possible to use the Startactivityforresult () method to open several different activities to handle different businesses, and when these new operations are closed, The system invokes the Onactivityresult (int requestcode, int resultcode, Intent data) method of the preceding activity. In order to know which new activity the returned data comes from, this can be done in the Onactivityresult () method (Resultactivity and newactivity for the new activity to be opened):

public class Resultactivity extends activity {
..... ResultActivity.this.setResult (1, intent);
ResultActivity.this.finish ();
}
public class Newactivity extends activity {
..... NewActivity.this.setResult (2, intent);
NewActivity.this.finish ();
}
public class Mainactivity extends activity {//at which the activity will open resultactivity and newactivity
@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
switch (resultcode) {case
1:
// Resultactivity return data for Case
2:
//newactivity return Data
}
}}

5 Demo Source:

Testresultactivity.java

Package mm.shandong.com.testresult;
Import android.content.Intent;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.EditText;
Import Android.widget.RadioButton;
Import Android.widget.RadioGroup;
Import Android.widget.TextView;
public class Testresultactivity extends appcompatactivity {edittext edittextbrand;
Radiogroup Radiogroup;
TextView Textviewxh; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_test_result);
Edittextbrand = (edittext) Findviewbyid (R.id.edittextbrand);
Radiogroup = (radiogroup) Findviewbyid (R.id.radiogroup);
Textviewxh = (TextView) Findviewbyid (R.ID.TEXTVIEWXH);
RadioButton Radionbutton = (RadioButton) radiogroup.getchildat (0);
Radionbutton.setchecked (TRUE); ///jump to select brand interface public void Selectbrand (view view) {Intent Intent = new Intent (this, testresultactivity1.class); startacti
Vityforresult (Intent, 1); }
///Select the computer you want to purchase public void Selectcompute (view view) {Intent Intent = new Intent (this, testresultactivity2.class);
String brand = Edittextbrand.gettext (). toString ();
RadioButton Radionbutton = (RadioButton) Radiogroup.findviewbyid (Radiogroup.getcheckedradiobuttonid ());
String NC = Radionbutton.gettext (). toString ();
Intent.putextra ("brand", brand);
Intent.putextra ("NC", NC);
Startactivityforresult (Intent, 2);
Textviewxh.settext ("");} The callback returned by the activity request @Override protected void Onactivityresult (int requestcode, int resultcode, Intent Intent) {switch (req Uestcode) {//resultcode for the postback of the tagged case 1:if (resultcode = = 2) {String brand = Intent.getstringextra ("brand"); Edittextbrand
. SetText (brand);
} break;
Case 2:if (ResultCode = = 3) {String XH = Intent.getstringextra ("XH"); Textviewxh.settext (XH); }
}
}

TestResultActivity1

Package mm.shandong.com.testresult;
Import android.content.Intent;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Android.widget.Toast;
public class TestResultActivity1 extends appcompatactivity {ListView ListView;
String[] Brands = new string[]{"Lenovo", "Dell"}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.LAYOUT.ACTIVITY_TEST_RESULT1);
ListView = (ListView) Findviewbyid (R.id.listview); Arrayadapter arrayadapter = new Arrayadapter (this, Android.
R.layout.simple_list_item_checked, brands);
Listview.setadapter (Arrayadapter);
Listview.setchoicemode (Listview.choice_mode_single); ///Submit selected branding results public void submit (view) {int index = listview.getcheckeditemposition (); if (Index < 0) {toast.ma
Ketext (This, "Please choose Brand", Toast.length_short). Show ();
Return } String brand = (string) listview.getiteMatposition (index);
int resultcode = 2;
Intent Intent = Getintent ();
Intent.putextra ("brand", brand);
Setresult (2, intent);
Finish (); }
}

TestResultActivity2

Package mm.shandong.com.testresult;
Import android.content.Intent;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Android.widget.Toast;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map; public class TestResultActivity2 extends appcompatactivity {ListView ListView; @Override protected void onCreate (Bundle s
Avedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_TEST_RESULT2);
Intent intent=getintent ();
String Brand=intent.getstringextra ("brand");
String Nc=intent.getstringextra ("NC");
Map Map=initdata ();
List<string> lists= (list<string>) map.get (BRAND+NC);
listview= (ListView) Findviewbyid (R.id.listview); Arrayadapter arrayadapter=new Arrayadapter (this, Android.
r.layout.simple_list_item_checked,lists);
Listview.setadapter (Arrayadapter); Listview.setchoicemode (ListView.choice_mode_single);
///Submit the selected computer model public void submit (view view) {int index= listview.getcheckeditemposition (); if (index<0) {
Toast.maketext (This, "Please select Model", Toast.length_short). Show ();
return;
String xh= (String) listview.getitematposition (index);
int resultcode=2;
Intent intent=getintent ();
Intent.putextra ("XH", XH);
Setresult (3,intent);
Finish ();
///initializes the data source public Map InitData () {map map=new HashMap ();
List<string> lists=new arraylist<> ();
Lists.add ("Lenovo 1g0001");
Lists.add ("Lenovo 1g0002");
Map.put ("Lenovo 1G", lists);
Lists=new arraylist<> ();
Lists.add ("Lenovo 2g0001");
Lists.add ("Lenovo 2g0002");
Map.put ("Lenovo 2G", lists);
Lists=new arraylist<> ();
Lists.add ("Dell 1g0001");
Lists.add ("Dell 1g0002");
Map.put ("Dell 1G", lists);
Lists=new arraylist<> ();
Lists.add ("Dell 2g0001");
Lists.add ("Dell 2g0002");
Map.put ("Dell 2G", lists);
return map; }
}

The above is a small set to introduce the activity examples of the start of the activity and return to the results, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.