首页 > 代码库 > StartActivity 和 StartActivityForResult
StartActivity 和 StartActivityForResult
startActivity 和 startActivityForResult都用来打开另外一个Activity,但是后者添加了更多功能:比如在MainActivity使用startActivityForResult打开了OtherActivity,那么调用OtherActivity的finish() 方法后,会自动调用MainActivity的 onActivityResult() 方法(需要从写),这样就可以做进一步的处理。当然不止在打开activity的时候可以通过intent传递数据,在关闭activity的时候,如果使用的是startActivityForResult,也可通过intent传递数据。我们来看一看实例:
1.startActivityForResult( Intent intent, int requestCode); 有两个参数,第一个参数intent和startActivity() 中作用一样,第二个参数表示请求码,主要是为了表示打开这个Activity的目的,比如说我在一个Activity中有两个按钮,需要打开同一个Activity,当关闭第二个Activity后返回第一个Activity,我需要处理的动作不一样,那么就需要判断requestCode。
比如例子中的:
btnJump1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(mContext, OtherActivity1.class); startActivityForResult(intent, 21); } });startActivityForResult( Intent intent, int request Code),第二个参数就是请求码,当关闭被打开Activity之后,这个requestCode会被原样返回到onActivityResult( int requestCode , ...) 的第一个参数。
2. onActivityResult(),当我使用startActivityForResult() 打开OtherActivity后,当我关闭OtherActivity,就会调用前一个Activity的onActivityResult() 方法。这里有3个参数,第一个requestCode 就是第一点讲的 设置的requestCode,第二个参数是在OtherActivity中设置的结果码,第三个参数也是在OtherActivity中设置的要传递的数据。
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { } }
3. resultCode 和 Intent data,这两个参数需要在被打开的Activity中设置,比如:
btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = mLoginBundle.getString("name"); String pass = mLoginBundle.getString("pass"); Intent data = http://www.mamicode.com/new Intent();>setResult(int resultCode, Intent data) ,这个方法就是用来返回的两个参数,当然具体的数据需要自己添加。
下面贴一下代码:
1. MainActivity.java
package com.example.demo_startactivityforresult; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private Context mContext = this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnJump1 = (Button) findViewById(R.id.btn_jumpto_other1); Button btnJump2 = (Button) findViewById(R.id.btn_jumpto_other2); Button btnJump3 = (Button) findViewById(R.id.btn_jumpto_other3); btnJump1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(mContext, OtherActivity1.class); startActivityForResult(intent, 21); } }); btnJump2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(mContext, OtherActivity2.class); startActivityForResult(intent, 22); } }); btnJump3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(mContext, OtherActivity3.class); Bundle bundle = new Bundle(); bundle.putString("name", "xiaoming"); bundle.putString("pass", "123456"); intent.putExtras(bundle); startActivityForResult(intent, 21); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.e(TAG, "requestCode:" + requestCode); Log.e(TAG, "resultCode:" + resultCode); Log.e(TAG, "data:" + data); if (requestCode == 21 && resultCode == 3) { Intent resultIntent = data; String result = data.getStringExtra("result"); Log.e(TAG, "result:" + result); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }2. OtherActivity1.java
package com.example.demo_startactivityforresult; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class OtherActivity1 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other1_layout); Button btnClose = (Button) findViewById(R.id.btn_close); btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setResult(1); finish(); } }); } }
3. OtherActivity2.javapackage com.example.demo_startactivityforresult; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class OtherActivity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other2_layout); Button btnClose = (Button) findViewById(R.id.btn_close); btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setResult(2); finish(); } }); } }
4. OtherActivity3.javapackage com.example.demo_startactivityforresult; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class OtherActivity3 extends Activity { private Bundle mLoginBundle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other3_layout); mLoginBundle = getIntent().getExtras(); Button btnClose = (Button) findViewById(R.id.btn_close); btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = mLoginBundle.getString("name"); String pass = mLoginBundle.getString("pass"); Intent data = http://www.mamicode.com/new Intent();>布局文件就是只有跳转activiyt的按钮,这就就不贴代码了。
下面是效果图:
StartActivity 和 StartActivityForResult