gpt4 book ai didi

android - 如何使用 onPause(后退按钮)发送结果 Intent

转载 作者:行者123 更新时间:2023-11-30 04:58:36 29 4
gpt4 key购买 nike

我有一个 Activity,它包含一个 Recycler View ,并使用一个额外的字符串从主 Activity 中调用。然后用户会做一些会改变字符串的事情,我想将它发送回 Main Activity 。有没有办法只使用手机上的后退按钮来做到这一点?我有结果 Intent 在我的第二个 Activity 的“onPause”方法中设置结果,但主要 Activity 一直说结果已取消。请帮忙!

主要 Activity 代码:


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

我的第二个 Activity :

@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 系统输出:

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}]

最佳答案

欢迎来到代码派对

首先我必须说一下你的问题的逻辑:

你想传递一些东西给一个 Activity ,它只是有意发生的。当您使用 Back btn 返回到 Activity 时,您将调用它的 onResume。

现在的解决方案:

  1. 您可以使用 Intent 覆盖 onBackedPressed,这样您就可以在 onResult 中收到您想要的内容...

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

您可以使用下面的链接了解更多关于 android 中的 lifeCycles

Life Cycling in android

在评论中更新我

关于android - 如何使用 onPause(后退按钮)发送结果 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58677186/

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