onActivityResult()不会被调用的活动 [英] onActivityResult() not being called in activity
问题描述
我已经看了几个例子,我找不到什么,我做错了。
I have looked at several examples and I cant find what I am doing wrong.
我的onActivityResult()方法不会被调用我的活动;
my onActivityResult() method is not being called on my activity;
TransactionFormActivity
正在启动一个名为新活动 VehicleSearchActivity
具有customListAdapter.when我点击在某个项目该适配器我想传递一个值回 TransactionFormActivity
。
TransactionFormActivity
is starting up a new activity called VehicleSearchActivity
which has a customListAdapter.when I click on an item in that adapter I want to pass a value back to the TransactionFormActivity
.
下面是我的两个活动code:
here is the code from my two activities:
code 的onClick()
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, TransactionFormActivity.class);
i.putExtra("VehicleId", rowItem.VehicleId);
i.putExtra("VehicleReg", rowItem.Registration);
context.startActivityForResult(i,0);
context.finish();
}
}
和这里是code在TransactionFormActivity
and here is the code in The TransactionFormActivity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
vehicleId = data.getIntExtra("VehicleId", 0);
vehicleReg.setText(data.getStringExtra("VehcilceReg"));
}
}
}
当我调试,并把破发点,我的code在onClickListener正在运行。然而,应用程序返回到TransactionFormActivty和 OnActivityResult()
方法不会被调用?
When I debug and put break points, my code in the onClickListener is being run. However the app returns to the TransactionFormActivty and the OnActivityResult()
method is never called?
我能是做错了。
推荐答案
您正在做一个小错误。
在你的FirstActivity你应该叫:
In your FirstActivity you should call:
//your code...
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
在你的SecondActivity你应该叫:
In your SecondActivity you should call:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
,然后回到你的FirstActivity使用onActivityResult来取回数据
and then back in your FirstActivity you use the onActivityResult to get the data back
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
这篇关于onActivityResult()不会被调用的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!