Method Analysis of Data Return Between Android Pages

  • 2021-07-22 11:24:50
  • OfStack

This paper describes the method of data return between Android pages. Share it for your reference, as follows:

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

Page 1 adds the following code:


Intent intent = new Intent();
intent.setClass( Page 1.this,  Page 2.class);
Bundle bundle = new Bundle();
intent.putExtras(bundle);// Will Bundle Add to Intent, It can also be found in Bundle Add the corresponding data to the next page , For example: bundle.putString("abc", "bbb");
startActivityForResult(intent, 0);//  Jump and ask for a return value, 0 Represents the request value ( You can write at will )

Page 2 Receive data add code as follows:


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

Page 1 receives return data: (onActivityResult needs to be rewritten)


@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();
  }
}

More readers interested in Android can check the topic of this site: "Android Thread and Message Mechanism Usage Summary", "activity Operation Skills Summary of Android Programming", "Android Debugging Skills and Common Problem Solutions Summary", "Android Development Introduction and Advanced Tutorial", "Android Multimedia Operation Skills Summary (Audio, Video, Recording, etc.)", "Android Basic Component Usage Summary", "Android View View Skill Summary", "Android Layout layout Skill Summary" and "Android Control Usage Summary"

I hope this article is helpful to everyone's Android programming.


Related articles: