一个activity 一个布局文件 不能跨越。
如果你需要使用main2.xml 需要另建activity
需要返回数据或结果的,则使用startActivityForResult (Intent intent, int requestCode),requestCode的值是自定义的,用于识别跳转的目标Activity。跳转的目标Activity所要做的就是返回数据/结果
setResult(int resultCode)只返回结果不带数据
setResult(int resultCode, Intent data)两者都返回!
而接收返回的数据/结果的处理函数是onActivityResult(int requestCode, int resultCode, Intent data),这里的requestCode就是startActivityForResult的requestCode,resultCode就是setResult里面的resultCode ,返回的数据在data里面。
注意,在setResult后,要调用finish()销毁当前的Activity,否则无法返回到原来的Activity ,就无法执行原来Activity的onActivityResult函数,看到当前的Activity没反应。
RESULT_OK和RESULT_CANCELED是系统空间里面的常量,不需要自定义,直接使用就行 。
另外在运行过程中,发现按Back键后,是可以返回RESULT_CANCELED的,看了SDK doc后,原来真的是那样,而且不带有数据的。这意味着,如果你设想在返回RESULT_CANCELED时并返回数据,那么需要截获Back键的事件处理,把原来返回RESULT_CANCELED的核心逻辑copy到事件处理里面。
public class MainActivity extends Activity { static final int CALL_MSG_REQUEST = 0; static final int CALL_BACK_REQUEST = 1; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == CALL_MSG_REQUEST){ if(resultCode == RESULT_OK){ Bundle bd = data.getExtras(); String name = bd.getString("name2"); Double salary = bd.getDouble("salary2"); TextView txt = (TextView)findViewById(R.id.txtMsg); txt.setText("Activity 1" + "\n名字:" + name + "\n薪水:" + String.valueOf(salary)); }else if (resultCode == RESULT_CANCELED) { Toast.makeText(this, "Send canceled!", Toast.LENGTH_SHORT).show(); } }else if(requestCode == CALL_BACK_REQUEST){ } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView v = (TextView)findViewById(R.id.txtMsg); v.setText("初始Text"); Button btn = (Button)findViewById(R.id.btn1); btn.setText("启动另一个Activity"); btn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { Intent ten = new Intent(); ten.setClass(MainActivity.this, SkipActivity.class); Bundle bubb = new Bundle(); bubb.putString("name", "ROBBY"); bubb.putDouble("salary", 100d); ten.putExtras(bubb); //startActivity(ten); startActivityForResult(ten, CALL_MSG_REQUEST); } }); } }
其实用startActivity(ten);可以返回,不过返回没带参数而已!
public class SkipActivity extends Activity { Intent ten = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_skip); ten = this.getIntent(); Bundle bd = ten.getExtras(); String name = bd.getString("name"); Double salary = bd.getDouble("salary"); TextView txt = (TextView) this.findViewById(R.id.textSkip); txt.setText("Activity 2" + "\n名字:" + name + "\n薪水:" + String.valueOf(salary)); Button btn = (Button) this.findViewById(R.id.btn2); btn.setText("返回前一个Activity"); btn.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // 返回参数给启动者 /*Bundle bd = ten.getExtras(); bd.putString("name2", "Robby2"); bd.putDouble("salary2", 120d); ten.putExtras(bd); SkipActivity.this.setResult(RESULT_OK, ten); */ Intent t = new Intent(); Bundle bd = new Bundle(); bd.putString("name2", "Robby2"); bd.putDouble("salary2", 120d); t.putExtras(bd); SkipActivity.this.setResult(RESULT_OK, t); //移上两种情况都可 SkipActivity.this.finish(); } }); } }
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/layout"> <TextView android:id="@+id/txtMsg" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
activity_skip.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textSkip" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
参考:
http://bbs.e800.com.cn/thread-4955-1-1.html
http://my.oschina.net/huangsm/blog/32035
http://blog.csdn.net/lixiang0522/article/details/7824490
本来看起来挺简单,结果自己实际操作起来,问题百出,典型的眼高手低啊,悲催