我正在使用一个简单的函数OnActivityResult,但它没有返回我想要的结果。
请查看我的代码并告诉我我在哪里做错了。
public void OnClickFunction(View view)
{
Intent intent = new Intent(getApplicationContext(), Second.class);
startActivityForResult(intent, RESULT_OK);
/// My actions. . .
}
然后在第二节课中,我设置了这样的结果:
Button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
ValueOne = EditText.getText().toString().trim();
if (ValueOne.equals(String.valueOf(Answer)))
{
Toast.makeText(getApplicationContext(), "Correct Answer", 0).show();
Second.this.setResult(RESULT_OK, null);
Second.this.finish();
}
else
{
Toast.makeText(getApplicationContext(), "Wrong Answer", 0).show();
}
}
});
现在回到第一个类,从那里调用Intent:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// if (requestCode == RESULT_OK)
// {
if (resultCode == RESULT_OK)
{
///// MyActions. . .
}
// }
}
调试器没有调试此功能,因此无法获得所需的结果 我在哪里做错了?
答案 0 :(得分:3)
你必须销毁第二项活动。尝试按后退按钮。我能够在onActivityResult
中看到所有日志消息第一项活动
public class FirstActivity extends Activity {
/** Called when the activity is first created. */
int result = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this,SecondActivity.class);
startActivityForResult(i, result);
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
Log.i("H", "RequestCode:" + requestCode);
Log.i("H", "ResultCode:" + resultCode );
}
}
SecondActivity
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(RESULT_OK);
Log.i("S","Exiting Second Activity");
}
}
答案 1 :(得分:2)
:
int activity=1;
Intent i=new Intent(Sourceclass.this,destination.class);
startActivityForResult(i,activity);
在目的地类:
Intent i=new Intent();
setResult(RESULT_OK,i);
finish();
在源类的OnActivityResult中:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if(requestCode==1)
{
Log.e("check","check");
}
}
}
答案 2 :(得分:0)
在我的情况下:使用activityForResult时,我添加了此标志:
addFlags(FLAG_ACTIVITY_NEW_TASK)
删除此标志时已修复