通過startActivityForResult跳轉(zhuǎn)Activity時,可以從跳轉(zhuǎn)的Activity拿到數(shù)據(jù)返回
代碼如下:
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//跳轉(zhuǎn)到第二個頁面
public void toSecond(View v ){
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String result = data.getExtras().getString("result");
Toast.makeText(this, "返回來的數(shù)據(jù)為:"+result, 1).show();
}
}
SecondActivity.java
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent data = new Intent();
data.putExtra("result", "我是obama");
setResult(1,data);
}
}