将setResult置于onPause(),onStop()或onDestroy()时,startActivityForResult无法正常工作

时间:2014-11-21 00:15:16

标签: android

当我将 setResult()放在按钮的 onClickListener 中时,这是有效的。但是,如果我把它放在 onPause() onStop() onDestroy()中,那就永远不会有效。

我真的很困惑。

这是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");
        }
    }
}

这是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();
    }
}

这是我在 onClick()

中放置 setResult()的日志
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

但是如果在 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

我不知道。

2 个答案:

答案 0 :(得分:2)

你也可以覆盖finish()并在调用超类之前设置结果:

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

答案 1 :(得分:0)

当用户执行选择结果的操作时,调用setResult()onPause()或更晚的生命周期事件为时已晚。

startActivityForResult()专为“挑选”类活动而设计,用户正在挑选某些东西。典型的模式是,一旦用户选择了他们的项目,您就可以拨打setResult()finish()