Question

I'm building an application where the first Activity starts another one by startActivityByResult. After some setting is done, I need to send the setting result back.

So I override the second Activity's onPause() method, I get the intent, putExra, and send it back through setResult().

Back to the first Activity. onActivityResult has definitely been called. I got the resultCode set before, but the intent data is null. I can't figure out what's wrong.

Here's my code:

The first Activity

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.e("page1", requestCode + "_");
    Log.e("page1", resultCode + "_");
    Log.e("page1", (data == null) + "_");
    // if ((requestCode == 1) && (data != null)) {
    if (data != null) {
        String size = data.getStringExtra("size");
        Log.e("switch", size + "_");
        switch (size.charAt(0)) {
        case '1': {
            text.setTextSize(10);
        }
            break;
        case '2': {
            text.setTextSize(20);
        }
            break;
        case '3': {
            text.setTextSize(30);
        }
            break;
        }
    }
}

My second Activity

    @Override
protected void onPause() {

    Intent intent = getIntent();
    Log.e("prefs", (intent == null) + "_");
    intent.putExtra("size", size);
    setResult(3, intent);
    super.onPause();
}

I've tested in LogCat. In the second Activity. The intent about to be sent is definitely not null. But when it goes to the first Activity. Null just returned. This is driving me really crazy.

Was it helpful?

Solution

Can you try this. you want to finish your activity in all the cases in pause manually and in back press automatically so it will be better to save your intent data in onDestroy

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

    @Override
    public void onDestroy() {
        Intent intent = getIntent();
        intent.putExtra("size", size);
        setResult(3, intent);
        super.onDestroy();
    }

OTHER TIPS

Problem with your code is in String size = data.getStringExtra("size"); this line. You should change it with String size = data.getExtras.getString("size");

In your first activity:

Intent intent = new Intent(context,SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE);

And in your second activity make a button called SAVE and in its listener;

        Intent result = new Intent();
        Bundle bundle = new Bundle();

        bundle.putString("keyName", "KeyValue");

        result.putExtras(bundle);
        setResult(RESULT_OK,result);
        finish();

And again in your first activity's onActivityresult method:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode==RESULT_OK){
        if(requestCode == REQUEST_CODE){
            Log.d("Activity Result", data.getExtras.getString("keyName");
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top