elricfeng
elricfeng

Reputation: 394

startActivityForResult can't work well when setResult is placed in onPause(), onStop() or onDestroy()

When I put setResult() in the onClickListener of a button, that works. However, if I put it in onPause(), onStop() or onDestroy(), that never works.

I got really confused about that.

Here is the code of MainActivity.

package com.example.hellotest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(getApplicationContext(), SecondActivity.class), 0);
            }
        });
    }

    @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK) {
            Toast.makeText(getApplicationContext(), "hello world", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "RESULT_OK");
        } else {
            Toast.makeText(getApplicationContext(), "result not ok", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "RESULT_CANCELED");
        }
    }
}

Here is the SecondActivity.

package com.example.hellotest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondActivity extends Activity {
    private static final String TAG = SecondActivity.class.getSimpleName();

    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
//              setResult(RESULT_OK);
                Log.d(TAG, "button on click");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }

    @Override
    protected void onDestroy() {
        setResult(RESULT_OK);
        Log.d(TAG, "onDestroy");
        super.onDestroy();
    }

    @Override
    protected void onPause() {
//      setResult(RESULT_OK);
        Log.d(TAG, "onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
//      setResult(RESULT_OK);
        Log.d(TAG, "onStop");
        super.onStop();
    }
}

And here is the Log of which I put setResult() in the onClick()

11-21 00:07:54.243: D/SecondActivity(4790): button on click
11-21 00:07:56.364: D/SecondActivity(4790): onPause
11-21 00:07:57.813: D/SecondActivity(4790): onStop
11-21 00:07:57.813: D/SecondActivity(4790): onDestroy

11-21 00:07:56.404: D/MainActivity(4790): RESULT_OK

But if in onDestroy()

11-21 00:10:08.125: D/SecondActivity(4846): onPause
11-21 00:10:09.456: D/SecondActivity(4846): onStop
11-21 00:10:09.456: D/SecondActivity(4846): onDestroy

11-21 00:10:08.176: D/MainActivity(4846): RESULT_CANCELED

I have no idea about that.

Upvotes: 0

Views: 1381

Answers (2)

Alex Lockwood
Alex Lockwood

Reputation: 83303

You can also override finish() and set the result before calling into the super class:

@Override
public void finish() {
    setResult(...);
    super.finish();   
}

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006634

Call setResult() when the user performs the operation that selects the result. onPause() or later lifecycle events are too late.

startActivityForResult() is designed for "picker" sorts of activities, where the user is picking something. The typical pattern is for you to call setResult() and finish() once the user has picked their item.

Upvotes: 0

Related Questions