Data exchange between activities two (Bundle)

Requirements: page 1 jumps to page 2, page 2 returns to page 1 and returns data

Add the following code to page 1:

 Intent intent = new Intent();
   intent.setClass(page1.this , page2.class ) ;
   Bundle bundle = new Bundle();
   intent.putExtras(bundle); // Add the Bundle to the Intent, or you can add the corresponding data to the Bundle and pass it to the next page,
   For example: bundle.putString("abc", "bbb"); 
   startActivityForResult(intent, 0); // Jump and ask for the return value, 0 represents the request value (you can write it casually)

 

Page 2 receives data and adds the code as follows:

  

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
bundle.putString( "aaa", "back"); // Add data to be returned to page 1 
intent.putExtras(bundle);
 this .setResult(Activity.RESULT_OK, intent); // Return to page 1 
this .finish( );

 

Page 1 receives the return data: (need to rewrite onActivityResult)

copy code
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            gameView.backString = bundle.getString("aaa");
             Toast.makeText(this, backString, Toast.LENGTH_SHORT).show();
        }

    }
copy code

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326962480&siteId=291194637