onactivityresult无法使用onbackpressed?

1bqhqjot  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(301)

我有两项活动, Activity1.java 以及 Activity2.java . Activity1.java 开始 Activity2.java 我需要 Activity2.java 将一些数据返回到 Activity1.java 当用户按下操作栏上的后退按钮时。但由于某种原因,它不起作用。。。
活动1.java:

public class Activity1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1);

        Intent intent = new Intent(this, Activity1.class);
        int requestCode = 100;

        startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Log.i("TEST", "RequestCode:" + requestCode);
        Log.i("TEST", "ResultCode:" + resultCode );
        switch (resultCode) {
            case RESULT_OK:
                Log.i("TEST", data.getStringExtra("MESSAGE"));
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

活动2.java

public class Activity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, Activity1.class);
        intent.putExtra("MESSAGE", "Hello from Activity 2!");
        Log.i("TEST", "Setting result...");
        setResult(RESULT_OK, intent);
        finish();
        super.onBackPressed();
    }
}

当我运行应用程序,只有“设置结果…”被记录到logcat。似乎还没有调用onactivityresult覆盖。我尝试过修改activity2的oncreate()方法中的请求代码、结果代码和设置结果,但是没有任何效果。
有人能帮忙吗?任何帮助都将不胜感激!

1aaf6o9v

1aaf6o9v1#

你的代码有3个问题。
1.从 Activity1 你自己开始不是吗 Activity2 . 更改代码

Intent intent = new Intent(this, Activity1.class);

Intent intent = new Intent(this, Activity2.class);

2.在 onBackPressed()Activity2 ,无需申报 Activity1 在意图中,从

Intent intent = new Intent(this, Activity1.class);

Intent intent = new Intent();
  1. In Activity1 ,你应该检查一下 requestCode 在进一步处理之前,请确保数据从正确的活动返回,因为您可能会启动多个活动。
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    Log.i("TEST", "RequestCode:" + requestCode);
    Log.i("TEST", "ResultCode:" + resultCode);
    if (requestCode == 100) {
        switch (resultCode) {
            case RESULT_OK:
                Log.i("TEST", data.getStringExtra("MESSAGE"));
                break;
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}
62o28rlo

62o28rlo2#

您没有正确调用意图:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_1);

    Intent intent = new Intent(this, Activity1.class);
    int requestCode = 100;

    startActivityForResult(intent, requestCode);
}

应该是:

Intent intent = new Intent(this, Activity2.class);
iovurdzv

iovurdzv3#

您需要在按下时返回结果 ActionBar 返回/主页按钮,但您在底部返回按钮中添加了代码。
所以,把密码转过来 Activity2ActionBar 后退按钮被按下,所以超控 onOptionsItemSelected ,并且“主页”按钮的id为 android.R.id.home .

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        Intent intent = new Intent(this, Activity1.class);
        intent.putExtra("MESSAGE", "Hello from Activity 2!");
        Log.i("TEST", "Setting result...");
        setResult(RESULT_OK, intent);
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

相关问题