读 http://www.cnblogs.com/lijunamneg/archive/2013/02/05/2892616.html 有感
文中提出了一个核心问题:
Android activity的setResult()在什么时候调用?
他给出了答案:
activity返回result是在被finish的时候,也就是说调用setResult()方法必须在finish()之前。
那么如果在如下方法中调用setResult()也有可能不会返回成功: onPause(), onStop(), onDestroy(),
因为这些方法调用不一定是在finish之前的,当然在onCreate()就调用setResult肯定是在finish之前的
什么意思呢:
假设我们有MainActivity和SecondActivity来一次跳转。
package com.example.setresult; import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build; public class MainActivity extends ActionBarActivity { private final static String TAG = MainActivity.class.getSimpleName(); final int requestCode=1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this,SecondActivity.class), requestCode); }
});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent arg2) {
Log.d(TAG, "resultCode="+resultCode);
switch (resultCode) {
case RESULT_OK:
Log.d(TAG, "ok");
break;
}
super.onActivityResult(requestCode, resultCode, arg2);
} @Override
protected void onRestart() {
Log.d(TAG, "MainActivity onRestart");
super.onRestart();
}
@Override
protected void onResume() {
Log.d(TAG, "MainActivity onResume");
super.onResume();
}
}
package com.example.setresult; import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build; public class SecondActivity extends ActionBarActivity {
private final static String TAG = SecondActivity.class.getSimpleName(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setResult(RESULT_OK);
} @Override
protected void onStop() { Log.d(TAG, "SecondActivity onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.d(TAG, "SecondActivity onDestroy");
super.onDestroy();
}
@Override
public void onBackPressed() { Log.d(TAG, "SecondActivity onBackPressed");
super.onBackPressed();
}
@Override
protected void onStart() {
Log.d(TAG, "SecondActivity onStart");
super.onStart();
}
@Override
protected void onPause() { Log.d(TAG, "SecondActivity onPause");
super.onPause();
}
}
07-30 20:15:37.291: D/SecondActivity(30684): SecondActivity onStart
07-30 20:15:39.151: D/SecondActivity(30684): SecondActivity onBackPressed
07-30 20:25:18.531: D/SecondActivity(32594): SecondActivity onPause
07-30 20:15:39.161: D/MainActivity(30684): resultCode=-1
07-30 20:15:39.161: D/MainActivity(30684): RESULT_OK
07-30 20:15:39.161: D/MainActivity(30684): MainActivity onRestart
07-30 20:15:39.161: D/MainActivity(30684): MainActivity onResume
07-30 20:15:39.501: D/SecondActivity(30684): SecondActivity onStop
07-30 20:15:39.501: D/SecondActivity(30684): SecondActivity onDestroy
当从SecondActivity按返回键的时候,发生以上日志。可以看见resultCode=-1是正确的
但是当你把
setResult(RESULT_OK);
这句话放到SecondActivity Onstop()中
就会发生以下日志:
07-30 20:19:31.521: D/SecondActivity(31785): SecondActivity onStart
07-30 20:19:35.031: D/SecondActivity(31785): SecondActivity onBackPressed
07-30 20:25:18.531: D/SecondActivity(32594): SecondActivity onPause
07-30 20:19:35.051: D/MainActivity(31785): resultCode=0
07-30 20:19:35.051: D/MainActivity(31785): MainActivity onRestart
07-30 20:19:35.051: D/MainActivity(31785): MainActivity onResume
07-30 20:19:35.381: D/SecondActivity(31785): SecondActivity onStop
07-30 20:19:35.381: D/SecondActivity(31785): SecondActivity onDestroy
resultCode=0 说明setResult没有成功
可见当onBackPressed事件发生后,activity会直接掉finish()方法,这时候resultCode就已经返回给前一个Activity了,所以就得到默认值resultCode=0
由此得出一个结论
setResult()调用必须要在OnPause()之前