如何使用onPause(返回按钮)发送结果意图

问题描述 投票:0回答:1

我有一个Activity,其中包含Recycler视图,并从主活动中调用了一个额外的字符串。然后,用户将执行一些更改字符串的操作,我想将其发送回Main活动。是否可以使用电话上的“后退”按钮来执行此操作?我有结果意图将结果设置为第二个活动的“ onPause”方法中,但主要活动一直在说结果已取消。请帮忙!

主要活动代码:


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 1){
            if(resultCode == RESULT_OK){
                pantryJson = data.getStringExtra("newPantryContents");
                pantry = new ArrayList<>(Arrays.asList(new Gson().fromJson(pantryJson,
                        Ingredient[].class)));
                System.out.println("onActivityResult says: " + pantryJson);
                savePantry();
            }
            if(resultCode == RESULT_CANCELED){
                System.out.println("onActivityResult says: result canceled");
                return;
            }
        }
    }

我的第二项活动:

@Override
    protected void onDestroy() {
        super.onDestroy();

        String pantryJsonArray = new Gson().toJson(pantry);
        System.out.println("ViewPantry onDestroy says: " + pantryJsonArray);
        Intent intent = new Intent(ViewPantryActivity.this,MainActivity.class);
        intent.putExtra("newPantryContents",pantryJsonArray);
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    protected void onPause() {
        String pantryJsonArray = new Gson().toJson(pantry);
        System.out.println("ViewPantry onPause Says" + pantryJsonArray);
        Intent resultIntent = new Intent(ViewPantryActivity.this,MainActivity.class);
        resultIntent.putExtra("newPantryContents",pantryJsonArray);
        setResult(ViewPantryActivity.RESULT_OK,resultIntent);
        super.onPause();

    }

LogCat System.Out:

2019-11-02 22:05:42.068 566-566/com.example.myapplication I/System.out: ViewPantry onPause Says[{"barcode":{"cornerPoints":[{"x":181,"y":360},{"x":193,"y":356},{"x":193,"y":479},{"x":181,"y":483}],"displayValue":"04904500","format":1024,"rawValue":"04904500","valueFormat":5},"image":{"mHeight":300,"mNativePtr":497431990208,"mWidth":300},"name":"Clicked","quantityInPantry":1},{"barcode":{"cornerPoints":[{"x":157,"y":359},{"x":229,"y":335},{"x":229,"y":461},{"x":157,"y":487}],"displayValue":"04904500","format":1024,"rawValue":"04904500","valueFormat":5},"image":{"mHeight":300,"mNativePtr":497433345344,"mWidth":300},"name":"Diet Coca-cola","quantityInPantry":3}]
2019-11-02 22:05:42.073 566-566/com.example.myapplication I/System.out: onActivityResult says: result canceled
2019-11-02 22:05:42.516 566-566/com.example.myapplication I/System.out: ViewPantry onDestroy says: [{"barcode":{"cornerPoints":[{"x":181,"y":360},{"x":193,"y":356},{"x":193,"y":479},{"x":181,"y":483}],"displayValue":"04904500","format":1024,"rawValue":"04904500","valueFormat":5},"image":{"mHeight":300,"mNativePtr":497431990208,"mWidth":300},"name":"Clicked","quantityInPantry":1},{"barcode":{"cornerPoints":[{"x":157,"y":359},{"x":229,"y":335},{"x":229,"y":461},{"x":157,"y":487}],"displayValue":"04904500","format":1024,"rawValue":"04904500","valueFormat":5},"image":{"mHeight":300,"mNativePtr":497433345344,"mWidth":300},"name":"Diet Coca-cola","quantityInPantry":3}]

android android-intent onactivityresult onpause android-ondestroy
1个回答
0
投票

欢迎参加密码聚会

首先,我必须说出您的问题的逻辑:

您想将某项内容传递给一项活动,它是有意发生的。当您使用Back btn返回活动时,请调用onResume。

现在的解决方案:

  1. 您可以使用意图覆盖onBackedPressed,以便可以在onResult中接收想要的内容...

2。您可以将代码的位置从onResult更改为onResume。

您可以使用下面的链接来了解有关android中生命周期的更多信息>

Life Cycling in android

在评论中更新我

© www.soinside.com 2019 - 2024. All rights reserved.