Get the data returned after the activity is closed.

Source: Internet
Author: User

If you want to get the data returned after the activity is closed, you need to use startactivityforresult (intent, int requestcode) (startactivity () provided by activity as the abstract method provided by context, the startactivityforresult method is a method unique to the activity itself.) The method opens a new activity. When the new activity is closed, the previous activity
To get the returned data, you must overwrite the onactivityresult (INT requestcode, intresultcode, intent data) method in the previous activity:

Publicclass mainactivityextends activity {

@ Override

Protectedvoid oncreate (bundle savedinstancestate ){

.......

Button button = (button) This. findviewbyid (R. Id. Button );

Button. setonclicklistener (New View. onclicklistener () {// click this button to open a new activity

Publicvoid onclick (view v ){


// The second parameter is the request code, which can be numbered based on business needs


Startactivityforresult (new intent (mainactivity. This, newactivity. Class), 1 );

}});

}

// The first parameter is the request code, that is, call startactivityforresult () to pass the previous value

// The second parameter is the result code, which identifies the new activity from which the returned data comes.

@ Override

Protected void onactivityresult (INT requestcode, int resultcode, intent data ){


String result = data. getextras (). getstring ("result"); // get the data returned after the new activity is disabled.

}

} When the new activity is disabled, the data returned by the new activity is transmitted through intent. The Android platform calls the onactivityresult () method of the previous activity, input the intent that stores the returned data as the third input parameter, and use the third input parameter in the onactivityresult () method to retrieve the data returned by the new activity.


Use the startactivityforresult (intent, int requestcode) method to open a new activity. Before closing a new activity, you must use the setresult (INT resultcode, intent data) provided by the system to return data to the previous activity) method implementation:

Public class
Newactivity extends activity {

@ Override

Protected void
Oncreate (bundle savedinstancestate ){

......

Button. setonclicklistener (New View. onclicklistener (){


Public void onclick (view v ){


Intentintent = new intent (); // data is returned using intent

 
Intent. putextra ("result", "Student cute"); // store the returned data to intent

 
Newactivity. This. setresult (result_ OK, intent); // sets the returned data.

Newactivity. This. Finish (); // close Activity

}});

}

}

The setresult () method is a special method of activity. The first parameter value can be defined according to business needs. The result_ OK used in the above Code is a constant defined by the system activity class, the value is-1, and the code snippet is as follows:

Public class
Android. App. Activity extends ......{

Public static final int result_canceled = 0;

Public staticfinal
Intresult_ OK =-1;

Public static final int result_first_user = 1;

}

Summary:

For example, if you want to open Activity B in activity A and want to transfer some data to activity a when Activity B is disabled,

1. Activity A needs to use its own startactivityforresult () method to open Activity B,

2. In Activity A, the onactivityresult (INT requestcode, int resultcode, intent data) method must be rewritten to accept the data transmitted by Activity B,

3. Use the setresult (INT resultcode, intent data) method in Activity B to set the returned data for activity.


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.