Startactivityforresult is used when passing values between multiple activity.

Source: Internet
Author: User

Requestcode request code, which calls Startactivityforresult () to pass past values ResultCode result codes, which are used to identify which new activity the returned data comes from

For example, it might be more clear:

1. Data returned after a newly opened activity has been closed in the activity

Package Com.google_acmer.activitys;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.widget.button;public class Mainactivity extends activity {Private final static String tag= ' mainactivity '; @Override public void onCreate (Bundle save Dinstancestate) {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 ("REsult ")//Get the data returned after the new activity closes LOG.I (TAG, result); }}

2. Need to return data to the previous activity before the new activity closes

Package Com.google_acmer.activitys;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.widget.button;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 Gu"); 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

The 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://Come From button 1 request, make the corresponding business process Case 2://Request from Button 2, make the corresponding business processing}}

The function 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 Onactivi Tyresult (int requestcode, int resultcode, Intent data) {switch (ResultCode) {Case 1://resultactivity return Data Case 2://New Activity's Return Data}}}

About Onactivityresult immediate response after calling Startactivityforresult, and Onactivityresult not responding when returning the current page

I was entangled in this problem for a long time, the second planted in the Launchmode ... Finally through the experiment to find out where this problem lies!

There are two kinds of cases:

First: When Resultcode==0, the launchmode of the activity must be set to "Singletask" or singleinstance, otherwise there will be an error. This is because, from a task's point of view, Android thinks that the activity between different tasks is not going to pass data, so you can't use the New_task identity, but still call the Forresult method.

Second: When Resultcode!=0, the launchmode of the activity must be standard (delete Launchmode property defaults to Standard mode), for the same reason.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.