Activity 之间传递参数和返回参数的方法
2016-08-17 17:32
351 查看
需要声明的包
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
1,传递参数的方法,在源Activity中实现如下:
{
Intent intent = new Intent();
Bundle bd = new Bundle();
bd.putInt("a", 0xa);
bd.putInt("b", 0xb);
bd.putInt("c", 0xC);
bd.putInt("d", 0xd);
bd.putInt("e", 0xe);
intent.putExtra("bundle", bd);
intent.putExtra(“test1”, "1");
intent.putExtra("test2", "2");
//startActivity(intent); /**不需要返回方式***/
startActivityForResult(intent,REQUEST_SEARCH_CODE);
}
2,获取参数的方法,在目的Activity中实现如下:
{
Intent intent = getIntent();
String test1 = intent.getStringExtra(“test1”);
String test2 = intent.getStringExtra(“test2”);
Bundle bd = intent.getBundleExtra("bundle");
int a = bd.getInt("a");
int b = bd.getInt("b");
int c = bd.getInt("c");
}
3,返回的方法;在上一个Activity中实现如下:
{
Intent intent = new Intent();
Bundle bd = new Bundle();
bd.putInt("a", 0xa);
bd.putInt("b", 0xb);
bd.putInt("c", 0xC);
bd.putInt("d", 0xd);
bd.putInt("e", 0xe);
intent.putExtra("bundle", bd);
intent.putExtra(“test1”, "1");
intent.putExtra("test2", "2");
setResult(resultCode,intent);
this.finish();
}
4,获取返回参数的方法(只限于startActivityForResult对应使用):在当前Activity中实现如下:
{
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "onActivityResult request:"+requestCode+", resultCode:" + resultCode + "\n");
switch(resultCode)
{
case A:
String test1 = data.getStringExtra(“test1”);
String test2 = data.getStringExtra(“test2”);
Bundle bd = data.getBundleExtra("bundle");
int a = bd.getInt("a");
int b = bd.getInt("b");
int c = bd.getInt("c");
break;
default:
break;
}
}
}
其中,requestCode为startActivityForResult(intent,REQUEST_SEARCH_CODE);的REQUEST_SEARCH_CODE的值;resultCode为上个ActivitysetResult(resultCode,intent)的resultCode,data为ActivitysetResult(resultCode,intent)的intent;
经常遇到的问题是,当前activity 没有堵塞,而是开了新的栈给新的Activity,导致马上返回的值不正确,原因是在AndroidManifest,xml android:launchMode设置的mode 为singleInstance,而是采用"standard";或是这种方式就不能用这种方式回传参数!
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
1,传递参数的方法,在源Activity中实现如下:
{
Intent intent = new Intent();
Bundle bd = new Bundle();
bd.putInt("a", 0xa);
bd.putInt("b", 0xb);
bd.putInt("c", 0xC);
bd.putInt("d", 0xd);
bd.putInt("e", 0xe);
intent.putExtra("bundle", bd);
intent.putExtra(“test1”, "1");
intent.putExtra("test2", "2");
//startActivity(intent); /**不需要返回方式***/
startActivityForResult(intent,REQUEST_SEARCH_CODE);
}
2,获取参数的方法,在目的Activity中实现如下:
{
Intent intent = getIntent();
String test1 = intent.getStringExtra(“test1”);
String test2 = intent.getStringExtra(“test2”);
Bundle bd = intent.getBundleExtra("bundle");
int a = bd.getInt("a");
int b = bd.getInt("b");
int c = bd.getInt("c");
}
3,返回的方法;在上一个Activity中实现如下:
{
Intent intent = new Intent();
Bundle bd = new Bundle();
bd.putInt("a", 0xa);
bd.putInt("b", 0xb);
bd.putInt("c", 0xC);
bd.putInt("d", 0xd);
bd.putInt("e", 0xe);
intent.putExtra("bundle", bd);
intent.putExtra(“test1”, "1");
intent.putExtra("test2", "2");
setResult(resultCode,intent);
this.finish();
}
4,获取返回参数的方法(只限于startActivityForResult对应使用):在当前Activity中实现如下:
{
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "onActivityResult request:"+requestCode+", resultCode:" + resultCode + "\n");
switch(resultCode)
{
case A:
String test1 = data.getStringExtra(“test1”);
String test2 = data.getStringExtra(“test2”);
Bundle bd = data.getBundleExtra("bundle");
int a = bd.getInt("a");
int b = bd.getInt("b");
int c = bd.getInt("c");
break;
default:
break;
}
}
}
其中,requestCode为startActivityForResult(intent,REQUEST_SEARCH_CODE);的REQUEST_SEARCH_CODE的值;resultCode为上个ActivitysetResult(resultCode,intent)的resultCode,data为ActivitysetResult(resultCode,intent)的intent;
经常遇到的问题是,当前activity 没有堵塞,而是开了新的栈给新的Activity,导致马上返回的值不正确,原因是在AndroidManifest,xml android:launchMode设置的mode 为singleInstance,而是采用"standard";或是这种方式就不能用这种方式回传参数!
相关文章推荐
- android activity之间传递参数,获取上一个activity的返回结果
- Asp.net页面之间传递参数的几种方法
- 页面之间传递参数得几种方法
- Asp.net页面之间传递参数的几种方法
- 页面之间传递参数得几种方法
- ASP.NET页面之间传递参数的几种方法荟萃(转)
- Asp.net页面之间传递参数的几种方法
- JavaScript实现页面之间传递参数的方法
- SQL中函数返回表,传递一个参数,无法唤醒用户自定义函数,解决方法!
- 页面之间传递参数的几种方法荟萃
- 页面之间传递参数的几种方法荟萃
- 窗口之间变量参数传递的实现方法
- Asp.net页面之间传递参数的几种方法
- Asp.net页面之间传递参数的几种方法
- 页面之间传递参数得几种方法
- Asp.net页面之间传递参数的几种方法
- Asp.net页面之间传递参数的几种方法荟萃
- Asp.net页面之间传递参数的几种方法荟萃
- Asp.net页面之间传递参数的几种方法
- 用javascript实现html页面之间的参数传递的四种方法