如何重用 startActivityforResult() 方法?
目前,当我单击按钮时,它使用方法 startActivityForResult();它找到答案并返回。
唯一的问题是一旦它返回,它将再次开始活动。我个人认为有一个只能运行一次的方法是完全没有意义的。当然必须有一个可以赋予该方法的标志,以便告诉它根据需要运行任意多次?
我已经阅读了 javadoc,它似乎没有帮助,因为它说它是一次性使用,除非我读错了?
在我的搜索活动中:
private OnItemClickListener listListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String text = (String) ((TextView) arg1).getText();
String[] selected = text.split(" - ");
selected[0] = selected[0].replace(' ', '_');
Log.w("COMPANY", selected[0]);
Log.w("PART", selected[1]);
Intent data = new Intent();
data.putExtra("key", selected);
setResult(RESULT_OK, data);
finish();
// startActivity(switch2);
}
};
在我的主要活动中 (IN 按钮监听器)
if (search.isPressed() && searchPressed == false) {
// show search list
Intent switch1 = new Intent(MainActivity.this, SearchActivity.class);
startActivityForResult(switch1, 0);
}
@Override
protected void onActivityResult(int req, int resp, Intent data) {
super.onActivityResult(req, resp, data);
searchPressed = true;
Bundle searched = data.getExtras();
String[] newItem = searched.getStringArray("key");
if (newItem[0].endsWith("_")) {
handleXML(1);
tv1.setText("Higher");
tv2.setText("Lower");
} else {
handleXML(0);
tv1.setText("Wear Resistance");
tv2.setText("Tougher");
}
competitors = h.competitors;
String[] piece = competitors.findCompanyParts(newItem);
assignMaterials(piece);
window.setVisibility(VISIBLE);
grade.setVisibility(INVISIBLE);
geo.setVisibility(INVISIBLE);
s1.setVisibility(INVISIBLE);
s2.setVisibility(INVISIBLE);
search.setVisibility(INVISIBLE);
help.setVisibility(INVISIBLE);
myTabHost.setCurrentTab(0);
}
Currently when I click my button it uses the method startActivityForResult(); It finds the answer and returns.
The only problem is once it returns it will start the activity again. Personally I think its completely pointless to have a method that can only be run once. Surely there must be a flag that can be given to the method in order to tell it to run as many times as wanted?
I've read the javadoc and it doesn't seem to help because it says its a one time use, unless I'm reading it wrong?
IN MY SEARCH ACTIVITY:
private OnItemClickListener listListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String text = (String) ((TextView) arg1).getText();
String[] selected = text.split(" - ");
selected[0] = selected[0].replace(' ', '_');
Log.w("COMPANY", selected[0]);
Log.w("PART", selected[1]);
Intent data = new Intent();
data.putExtra("key", selected);
setResult(RESULT_OK, data);
finish();
// startActivity(switch2);
}
};
IN MY MAIN ACTIVITY
(IN Button LISTENER)
if (search.isPressed() && searchPressed == false) {
// show search list
Intent switch1 = new Intent(MainActivity.this, SearchActivity.class);
startActivityForResult(switch1, 0);
}
@Override
protected void onActivityResult(int req, int resp, Intent data) {
super.onActivityResult(req, resp, data);
searchPressed = true;
Bundle searched = data.getExtras();
String[] newItem = searched.getStringArray("key");
if (newItem[0].endsWith("_")) {
handleXML(1);
tv1.setText("Higher");
tv2.setText("Lower");
} else {
handleXML(0);
tv1.setText("Wear Resistance");
tv2.setText("Tougher");
}
competitors = h.competitors;
String[] piece = competitors.findCompanyParts(newItem);
assignMaterials(piece);
window.setVisibility(VISIBLE);
grade.setVisibility(INVISIBLE);
geo.setVisibility(INVISIBLE);
s1.setVisibility(INVISIBLE);
s2.setVisibility(INVISIBLE);
search.setVisibility(INVISIBLE);
help.setVisibility(INVISIBLE);
myTabHost.setCurrentTab(0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你肯定做错了什么。
该方法只是启动另一个活动。 startActivity() 和 startActivityForResult() 之间没有真正的区别,除了第二个可以选择附加到回调,该回调将在启动的 Activity 完成后侦听一些返回值。
您应该粘贴一些代码来向我们展示您在做什么,我相信我们会很快指出您出错的地方。
You are definitely doing something wrong.
The method simply launches another activity. There is no real difference between startActivity() and startActivityForResult() other than that the second one has the option of being attached to a callback that will listen for some returned values from the launched Activity once that one has finished.
You should paste in some code to show us what you're doing and I'm sure we'll quickly point out where you've gone wrong.
您需要了解它是一个回调方法,用于在活动之间获取结果。
因此您无法设置应该调用它的次数。
但是如果您想重用其中获得的数据,请保存您第一次获得的意图/数据并执行您想要的任何操作。
You need to understand that its a callback method used to get the result in between the activities.
So there is no point of you being able to set how many times it should be called.
But if you want to reuse the data you got in it, then save that intent/data you get in it the first time and do whatever you wish.