Android Learning Summary Gets the data _android the activity returned by the activity that was started

Source: Internet
Author: User

Current activity: Contains a button and a textview for starting another activity and displaying the returned data, where the Onactivityresult () method is rewritten.

public class Mainactivity extends Appcompatactivity {
private TextView TextView;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Find TextView
textview= (TextView) Findviewbyid (R.id.textview);
Findviewbyid (r.id.btnsend). Setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick ( View view) {
Intent intent=new Intent (mainactivity.this,anotheractivity.class);
Startactivityforresult (intent,0);
}
);
@Override
protected void onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
Textview.settext ("Another activity returns:" +data.getstringextra ("Data"));
}

XML file: Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <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"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_ Margin "
android:paddingbottom=" @dimen/activity_vertical_margin "
tools:context=" Com.example.androidtest.MainActivity ">
<button
android:layout_width=" Wrap_content "
android:layout_height= "Wrap_content"
android:text= "start another activity"
android:id= "@+id/btnsend"/>
<textview
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
android:text= "New text"
android:id= "@+id/textview"/>
</LinearLayout>

Activity initiated: Contains a button and edittext for sending data back and entering data to be transmitted.

public class Anotheractivity extends Appcompatactivity {
private edittext edittext;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_another);
edittext= (EditText) Findviewbyid (r.id.edittext);
Button button= (button) Findviewbyid (r.id.btnsendback);
Button.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (view view) {
// Return results
Intent i=new Intent ();
I.putextra ("Data", Edittext.gettext (). toString ());
Setresult (1,i);
Finish ();}}
);
}

XML file: Activity_another.xml

<?xml version= "1.0" encoding= "Utf-8"?> <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"
android:paddingbottom= "@dimen/activity_vertical_margin"
android: paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
android:orientation= "vertical"
tools: context= "Com.example.androidtest.AnotherActivity" >
<edittext
android:layout_width= "Match_ Parent "
android:layout_height=" wrap_content "
android:id=" @+id/edittext "/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:text= "send Back"
Android:id= "@+id/btnsendback"/>

Run Result:

Add: Here Click Send Back button to return to the previous activity no problem, but if the point system comes with the return key will be wrong, the reason for this bug is ResultCode did not judge, click the system with the return key resultcode== Result_canceled, so it's not the same.

Solution: So here's Requestcode and ResultCode can play a role in the above procedure requestcode==0,resultcode== 1, that is, to determine whether to jump to the activity and to return the previous activity through the button or through the system return key, so the Onactivityresult () method in mainactivity can be improved to:

@Override
protected void onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (requestcode==0) {
if (resultcode==1) {
Textview.settext ("The data returned by another activity is:" +data.getstringextra (" Data "));}}

The above is a small set to introduce you to get the activity returned by the start of the data, hope to help everyone, 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.