We will pass data back to MainActivity.
In class Second, we want to pass a string to MainActivity.
In MainActivity, change code in button Next to open class second to like
this.
Intent
in=new Intent(getBaseContext(),second.class);
Bundle bun = new Bundle();
bun.putString("text",a );
bun.putInt("num", number);
bun.putStringArray("gi", fru);
in.putExtras(bun);
startActivityForResult(in, 1);
We use startActivityForResult to wait for
something pass back .
Copy to above last bracket.
@Override
protected void
onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == 3){
Bundle bun = data.getExtras();
String back = bun.getString("iz");
tv.setText(back); }
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
In this Override command, we receive
string and set to a textView. Take notice to resultCode == 3.
In class second, copy in to Back button,
above finish().
String back=”This is
string send back”;
Intent in = getIntent();
Bundle bun = new Bundle();
bun.putString("iz", back);
in.putExtras(bun);
setResult(3, in);
Take notice to setResult(3, in);
It must be the same with number in resultCode
in class MainActivity.
Keyword “iz” the same too.
Save and run to see result.
No comments:
Post a Comment