How to send result Intent using onPause (back button)

175 Views Asked by At

I have an Activity that contains a Recycler view and is called from the main activity with an extra string. The user would then do some things that would change the string and I would like to send it back to the Main activity. Is there a way to do this just using the back button on the phone? I have the result intent setting the result in the "onPause" method of my second activity, but the main activity keeps saying the result was canceled. Please help!

Main Activity code:


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

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

1

There are 1 best solutions below

1
On BEST ANSWER

welcome to the code party

First i must say logic of your issue:

You want to pass something to an activity, its just happened with intent. when you get back to an activity with Back btn, you call onResume of this.

Now solutions :

  1. you can override onBackedPressed with an intent so you can receive what you want in onResult...

2.you can change location of your code from onResult to onResume.

you can use the link below to learn more about lifeCycles in android

Life Cycling in android

Update me in comments