gpt4 book ai didi

android - OnActivityResult 不工作(Android)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:55 24 4
gpt4 key购买 nike

我正在使用一个简单的函数 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();
}
}
});

现在回到 first.class,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. . .
}
// }
}

调试器没有调试这个函数,所以没有得到想要的结果。
我哪里做错了??

最佳答案

您必须销毁第二个 Activity 。尝试按后退按钮。我能够在 onActivityResult 中看到所有日志消息

第一个 Activity

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 );
}
}

第二个 Activity

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");
}
}

关于android - OnActivityResult 不工作(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8587769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com