Activity jump & Bundle transfer data, androidactivity jump
There are two redirect methods for an Activity:
1. startActivity (Intent intent), directly start other activities
btnChange = (Button)findViewById(R.id.btnChange);btnChange.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent mIntent = new Intent();mIntent.setClass(MainActivity.this, AnotherActivity.class);startActivity(mIntent);}});
2. startActivityForResult (Intent intent, int requestCode), start the Activity with the specified request code, and the program will call onActivityResult after starting the new Activity to obtain the results passed back by the new Activity.
2.1-Bundle: putExtras (Bundle data)/getExtras (), putExtra (String name, Xxx value)/getXxxExtra (String name)
I. the btn in MainActivity calls startActivityForResult:
btnChange = (Button)findViewById(R.id.btnChange);btnChange.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent mIntent = new Intent(MainActivity.this, AnotherActivity.class);startActivityForResult(mIntent,0);}});II. In MainActivity, rewrite the data returned by the onActivityResult listener.
@ Overridepublic void onActivityResult (int requestCode, int resultCode, Intent intent) {if (requestCode = 1 & resultCode = 1) {Bundle data = intent. getExtras (); // result is the valueString result = data whose key is Name returned by AnotherActivity. getString ("Name ");}}III. Send data in AnotherActivity
BtnSend = (Button) findViewById (R. id. btnSend); btnSend. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Intent intent = getIntent (); intent. putExtra ("Name", "John"); // sets ResultCodeAnotherActivity. this. setResult (0); AnotherActivity. this. finish ();}});