Home > Mobile >  Use startActivityForResult () method returns the data
Use startActivityForResult () method returns the data

Time:10-07


Example, in FirstActivity jump to SecondActivity after clicking the Button, and then click the back Button to return to the FirstActivity, and returns a string,

1. FirstActivity sending code is as follows:

StartActivityForResult (intent, 1);

2. SecondActivity, rewrite when click on the return key trigger onBackPressed () method, the code is as follows:

@ Override
Public void onBackPressed () {
Intent Intent=new Intent ();
Intent. PutExtra (" data_return back ", "first activity");
SetResult (RESULT_OK, intent);
finish();
}
3. When returning from SecondActivity, will back on an activity's onActivityResult () method, so you need to rewrite FirstActivity onActivityResult () method to get the data returned, the code is as follows:

@ Override
Protected void onActivityResult (int requestCode, int the resultCode, Intent data) {
The switch (requestCode) {
Case 1:
If (the resultCode==RESULT_OK) {
String backData=https://bbs.csdn.net/topics/data.getStringExtra (" data_return ");
The d (" FirstActivity, "backData);
}
break;
Default:
}
}

CodePudding user response:

What's the problem? You say your question
  • Related