我有一个主要活动和一个preferenceActivity.在我的第一个活动中,我调用menu并通过调用startActivityForResult继续使用preferenceActivity.
case R.id.settings:
startActivityForResult(new Intent(this,SettingsActivity.class),LAUNCH_SETTINGS);
return true;
然后我更改我的设置并希望返回主要活动,并查看应用了新设置的主要活动.在onPause()方法中执行以下操作(因为我正确理解当按下后退按钮时会调用此方法,对吧?)
@Override
protected void onPause() {
super.onPause();
setResult(RESULT_OK,new Intent(this,MainActivity.class));
finish();
}
关于主要活动
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
if (requestCode == LAUNCH_SETTINGS) {
if (resultCode == RESULT_OK) {
new RefreshList().execute(ACTION_SELECT);
Log.d(TAG,"On activity result");
}
}
}
但是我的acyncTask没有调用并且没有打印日志.我怎么能正确地做到这一点?
谢谢!
On my first activity I call menu and go on preferenceActivity by calling startActivityForResult.
将startActivityForResult()与PreferenceActivity一起使用是相当不寻常的. PreferenceActivity旨在与startActivity()一起使用.如果启动PreferenceActivity的人关心首选项更改,它应该使用SharedPreferences对象注册首选项更改侦听器.
Then I change my settings and want to return on main activity and see main activity with new settings applyed.
我建议您使用SharedPreferences.OnSharedPreferenceChangeListener.或者,只需在原始活动的onStart()或onResume()方法中重新读取您关注的首选项.
In onPause() method do following (as I right understand this method will be called when I press back button,right?)
不,那不行. onPause()为时已晚,无法调用setResult().