传递数据时,返回的主要活动(意向),我做了什么错? [英] passing data when returning to main activity(Intent) what I've done wrong?
问题描述
我是新来的机器人。这可能是所有的最简单的问题!但我无法弄清楚什么乱子这里,
I'm new to android . This might be the simplest question of all !! but I couldn't figure out whats gone wrong here,
我试图创建一个基本的例子,通过intent.So传递价值观,我需要将数据传递到时候我在这里结束我的第二个活动主要活动是code
I was trying to create a basic example for passing values through intent.So I need to pass data to Main Activity when I close my Second Activity here is the code
IntentTest1(MainActivity)
IntentTest1(MainActivity)
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyClass.myToast("Clicked",getApplicationContext());
Intent myIntent = newIntent(getApplicationContext(),SecondPage.class);
startActivityForResult(myIntent,0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == 0 && resultCode == RESULT_OK)
if(data.hasExtra("title"))
{
MyClass.myToast(""+resultCode+""+requestCode, getApplicationContext());
String str = data.getExtras().getString("title").toString();
titleText.setText(str);
}
super.onActivityResult(requestCode, resultCode, data);
}
SeconPage
SeconPage
public void finish()
{
Intent returnIntent = new Intent(getApplicationContext(),Intenttest1.class);
returnIntent.putExtra("Welcome Back!!","title");
setResult(RESULT_OK, returnIntent);
// below was for tosting and it works!!
MyClass.myToast("finally",getApplicationContext());
super.finish();
}
我认为这是在接收的意图一些错误,我无法弄清楚。 需要解答,并建议 在此先感谢
I think there is some mistake in receiving the intent ,I couldn't figure out. Answers and Advises are needed thanks in advance
推荐答案
当你创建的第一个问题是你的意图
发回第一活动
。由于您使用的 startActivityForResult()
要使用一个空的构造。因此,改变
The first problem is when you create your Intent
to send back to the first Activity
. Since you are using startActivityForResult()
you want to use an empty constructor. So change
Intent returnIntent = new Intent(getApplicationContext(),Intenttest1.class);
到
Intent returnIntent = new Intent();
第二个问题是,你有你的键/值
对向后在其他
。该键
,这是你的样子与 getStringExtra()
等...应该是首先在对。所以这个
The second problem is that you have your key/value
pair backwards in your Extras
. The key
, which is what you look for with getStringExtra()
etc... should be the first in the pair. So this
returnIntent.putExtra("Welcome Back!!","title");
应
returnIntent.putExtra("title", "Welcome Back!!");
题外话
我会考虑使用相关的名称作为你的 PARAMS
。例如,我会改变你的的onClick()
从
I would consider using relevant names as your params
. For example, I would change your onClick()
from
public void onClick(View arg0)
到
public void onClick(View view)
视图
, v
,或者类似的东西更有意义,因为参数实际上是一个视图,这将是更可读
view
, v
, or something similar makes more sense since the argument actually is a view and it will be more readable
我也推荐使用活动
上下文
您意图
,您可以从参数获得(在查看
)传递到的onClick()
。因此,将其更改为
I would also recommend using the Activity
Context
for your Intent
which you can get from the argument (the View
) passed into onClick()
. So change it to
public void onClick(View v)
{
MyClass.myToast("Clicked",getApplicationContext());
Intent myIntent = newIntent(v.getContext(),SecondPage.class);
startActivityForResult(myIntent,0);
这篇关于传递数据时,返回的主要活动(意向),我做了什么错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!