在一個主界面(主Activity)上能連接往許多不同子功能模塊(子Activity上去),當子模塊的事情做完之后就回到主界面,還同時返回一些子模塊完成的數據交給主Activity處理。 用startActivity啟動主界面是一個新的Intent實例,而訪問的主界面還在activity棧的下面沒有調出來,這樣做的一個最大的問題是,回不到原界面就不能多個子功能模塊共同來給主界面提供數據或服務了。 目的: A.java 是主界面,B.java 是子功能模塊,要從A啟動B,B干完活之后把結果匯報給A 先看 A.java 的相關代碼 //--?A.java?--// //啟動B Intent bintent = new Intent(A.this, B.class); //設置 bintent的Bundle的一個值 String bsay = "Hello, this is B speaking";? bintent.putExtra("listenB", bsay) startActivityForResult(bintent,0); // 參數(Intent intent, Int requestCode) 的 requestCode 對應下面回收Bundle時識別用的 //重寫onActivityResult()來處理返回的數據,建議先看B.java 的代碼再回來看這里比較好理解 //這理有三個參數 requestCode, resultCode, data protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { //resultCode為回傳的標記,我在B中回傳的是RESULT_OK case RESULT_OK: Bundle b=data.getExtras(); ?//data為B中回傳的Intent String str=b.getString("ListenB");//str即為回傳的值"Hello, this is B speaking" break; default: break; } } ------------------------------------------------------------------------------------- //-- B.java?--// // 用?setResut() 準備好要回傳的數據后,只要使用finish()的方法就能把打包好的數據發給A且運行onActivityResult()部分的代碼 Intent aintent =?new Intent(B.this, A.class); setResut(RESULT_OK,aintent); //這理有2個參數(int resultCode, Intent intent) ... ... finish();
startActivityForResult(Intent intent, Int requestCode) intent 傳給B,requestCode >=0,隨便用于在onActivityResult()區別哪個子模塊回傳的數據,如果還有C.java ,D甚至E子模塊的話,每個區分開不同的requestCode就好。
setResut(int resultCode, Intent intent) resultCode 如果B子模塊可能有幾種不同的結果返回,可以用這個參數予以識別區分。這里還有個特殊的 RESULT_OK 值,沒有特殊情況用它就好。
onActivityResult(int requestCode, int resultCode, Intent intent) 這里三個都不用解釋了,與上文對應的東西。如果不對requestCode和resultCode 加以識別區分的話,只要有其他activity setResult到了A ?onActivityResult()會無差別處理。 // ************************************************************************************************************ /
首先看結構 這個結構由三部分組成:
startActivityForResult
onActivityResult()
setResult()
這個三個函數構成
具體的參數就不寫了 應該能知道。
注意的是調用setResult之后必須調用finish()不然的話 是不會跳轉的
調用setResult的時候直接調用即將回跳的函數的
onActivityResult() 然后在調用該函數的onrestart onstart onresume? 如果你在每個setResult后下finish方法 那么該activity直接調用destory方法。
第一個activity
?public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
??????? Log.v("startforrequest", "onCreate:"+this.getLocalClassName());
??????? Button bt=(Button)findViewById(R.id.bt);
??????? bt.setOnClickListener(new OnClickListener(){
???@Override
???public void onClick(View v) {
????// TODO Auto-generated method stub
????Intent intent=new Intent(StartForResultTest.this,StartForResultTest1.class);
????startActivityForResult(intent, 0);
???}});
??? }
???
???
??? @Override
??? protected void onActivityResult(int requestCode, int resultCode, Intent data) {
??? ?// TODO Auto-generated method stub
??? ?super.onActivityResult(requestCode, resultCode, data);
??? ?Log.v("startforrequest", "onActivityResult: "+requestCode+" : "+resultCode);
??? }
?
第二個activity
? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main1);
??????? Log.v("startforrequest", "onCreate:"+this.getLocalClassName());
??????? Button bt=(Button)findViewById(R.id.bt);
??????? bt.setOnClickListener(new OnClickListener(){
???@Override
???public void onClick(View v) {
????// TODO Auto-generated method stub
????Intent intent=new Intent(StartForResultTest1.this,StartForResultTest2.class);
????startActivityForResult(intent, 1);
???}});
??? }
???
???
??? @Override
??? protected void onActivityResult(int requestCode, int resultCode, Intent data) {
??? ?// TODO Auto-generated method stub
??? ?super.onActivityResult(requestCode, resultCode, data);
??? ?Intent intent=new Intent(this,StartForResultTest.class);
??? ?setResult(1, intent);
??? ?finish();
??? ?Log.v("startforrequest", "onActivityResult: "+requestCode+" : "+resultCode);
??? }
?
第三個也就是最后一個activity
? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main2);
??????? Log.v("startforrequest", "onCreate:"+this.getLocalClassName());
??????? Button bt=(Button)findViewById(R.id.bt);
??????? bt.setOnClickListener(new OnClickListener(){
???@Override
???public void onClick(View v) {
????// TODO Auto-generated method stub
????Intent intent=new Intent(StartForResultTest2.this,StartForResultTest1.class);
????? ?setResult(2, intent);
????? ?finish();
???}});
??? }
那么當跳轉到最后一個的時候 回直接調回第一個activity 調用restart start resume方法之后
再destory 最后一個 倒數第二個..
/
?
startActivityForResult Demo
startActivityForResult(Itent intent, int requestCode)用來喚起一個Activity。利用這個方法,前一個Activity1便會有一個等待次Activity2的返回,返回時響應onActivityResult(int requestCode, int resultCode, Intent data)方法。
? Demo如下:
? 第一個activity
?
Java代碼 ?
package?com.kevin.intent; ????import?android.app.Activity; ??import?android.content.Intent; ??import?android.os.Bundle; ??import?android.view.View; ??import?android.view.View.OnClickListener; ??import?android.widget.Button; ??import?android.widget.EditText; ????public?class?Main?extends?Activity?implements?OnClickListener{ ??????private?Button?btn_transfer; ??????private?EditText?et_name; ??????private?int?requestCode?=?1; ????????????@Override??????public?void?onCreate(Bundle?savedInstanceState)?{ ??????????super.onCreate(savedInstanceState); ??????????setContentView(R.layout.main); ??????????btn_transfer?=?(Button)?findViewById(R.id.button1); ??????????et_name?=?(EditText)?findViewById(R.id.editText1); ??????????btn_transfer.setOnClickListener(this); ??????} ????????@Override??????protected?void?onActivityResult(int?requestCode,?int?resultCode,?Intent?data)?{ ??????????System.out.println("requestCode--->"?+?requestCode); ????????????????????switch?(resultCode)?{ ??????????case?RESULT_OK: ????????????????????????????Bundle?returnedBundle?=?data.getExtras(); ??????????????String?returnedName?=?returnedBundle.getString("name"); ??????????????et_name.setText(returnedName); ??????????????break; ????????????default: ??????????????break; ??????????} ??????} ????????@Override??????public?void?onClick(View?v)?{ ????????????????????String?name?=?et_name.getText().toString(); ??????????Bundle?bundle?=?new?Bundle(); ??????????bundle.putString("name",?name); ??????????Intent?intent?=?new?Intent(); ??????????intent.setClass(this,?Activity_1.class); ??????????intent.putExtras(bundle);??????? ????????????????????startActivityForResult(intent,?requestCode); ??????} ?????? ??}?? package com.kevin.intent;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;public class Main extends Activity implements OnClickListener{private Button btn_transfer;private EditText et_name;private int requestCode = 1;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btn_transfer = (Button) findViewById(R.id.button1);et_name = (EditText) findViewById(R.id.editText1);btn_transfer.setOnClickListener(this);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {System.out.println("requestCode--->" + requestCode);// resultCode是第二個Activity返回過來的值switch (resultCode) {case RESULT_OK:// 獲取Bundle對象Bundle returnedBundle = data.getExtras();String returnedName = returnedBundle.getString("name");et_name.setText(returnedName);break;default:break;}}@Overridepublic void onClick(View v) {// 取得et_name的值String name = et_name.getText().toString();Bundle bundle = new Bundle();bundle.putString("name", name);Intent intent = new Intent();intent.setClass(this, Activity_1.class);intent.putExtras(bundle); // 啟動Activity_1startActivityForResult(intent, requestCode);}}
? 跳轉后的activity
Java代碼 ?
package?com.kevin.intent; ????import?android.app.Activity; ??import?android.content.Intent; ??import?android.os.Bundle; ??import?android.view.View; ??import?android.view.View.OnClickListener; ??import?android.widget.Button; ??import?android.widget.TextView; ????public?class?Activity_1?extends?Activity?implements?OnClickListener{ ??????private?Button?btn_return; ??????private?TextView?textView; ??????private?Intent?intent; ????????????@Override??????public?void?onCreate(Bundle?savedInstanceState)?{ ??????????super.onCreate(savedInstanceState); ??????????setContentView(R.layout.activity1); ??????????intent?=?this.getIntent(); ??????????Bundle?bundle?=?intent.getExtras(); ??????????String?name?=?bundle.getString("name"); ??????????System.out.println("name?---?>"?+?name); ??????????textView?=?(TextView)?findViewById(R.id.tv_receivedname); ????????????textView.setText("傳過來的name值為:?"?+?name); ?????????? ??????????btn_return?=?(Button)?findViewById(R.id.btn_return); ??????????btn_return.setOnClickListener(this); ??????} ??????@Override??????public?void?onClick(View?v)?{ ????????????????????setResult(RESULT_OK,?intent); ??????????this.finish(); ??????} ??}?? package com.kevin.intent;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class Activity_1 extends Activity implements OnClickListener{private Button btn_return;private TextView textView;private Intent intent;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity1);intent = this.getIntent();Bundle bundle = intent.getExtras();String name = bundle.getString("name");System.out.println("name --- >" + name);textView = (TextView) findViewById(R.id.tv_receivedname);textView.setText("傳過來的name值為: " + name);btn_return = (Button) findViewById(R.id.btn_return);btn_return.setOnClickListener(this);}@Overridepublic void onClick(View v) {// 點擊返回按鈕后,設置返回值setResult(RESULT_OK, intent);this.finish();}
}
- IntentDemo3.zip (45.2 KB)
?
總結
以上是生活随笔為你收集整理的startActivityForResult()的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。