本文介绍了如何从preference屏幕返回主活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要活动和一个preferenceActivity。在我的第一个活动我调用菜单,并通过调用startActivityForResult去preferenceActivity。

 案例R.id.settings:
                startActivityForResult(新意图(这一点,SettingsActivity.class),LAUNCH_SETTINGS);
                返回true;
 

然后我改变我的设置,并希望在主要活动回来,看到applyed新设置的主要活动。在的onPause()方法做下(如我正确的理解,当我preSS后退按钮这个方法会被调用,对吧?)

  @覆盖
     保护无效的onPause(){
         super.onPause();
         的setResult(RESULT_OK,新意图(这一点,MainActivity.class));
         完();
     }
 

在主要活动

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(要求code == LAUNCH_SETTINGS){
            如果(结果code == RESULT_OK){
                新RefreshList()执行(ACTION_SELECT);
                Log.d(TAG,在活动的结果);
            }
        }
    }
 

但我acyncTask不叫和日志不打印。我可以如何正确地做到这一点?谢谢!

解决方案

使用 startActivityForResult() preferenceActivity 是相当不寻常。 preferenceActivity 被设计为与 startActivity使用()。如果一开始 preferenceActivity 在乎preference变化,就应该注册一个preference变化监听与共享preferences 对象。

我推荐你使用共享preferences.OnShared preferenceChangeListener 代替。或者,干脆重新阅读你所关心的在原有活动的 ONSTART() onResume()方法。

没有,这是行不通的。 的onPause()来不及叫的setResult()

I have one main activity and one preferenceActivity. On my first activity I call menu and go on preferenceActivity by calling startActivityForResult.

                case R.id.settings:
                startActivityForResult(new Intent(this, SettingsActivity.class), LAUNCH_SETTINGS);
                return true;

Then I change my settings and want to return on main activity and see main activity with new settings applyed. In onPause() method do following (as I right understand this method will be called when I press back button, right?)

   @Override
     protected void onPause() {
         super.onPause();
         setResult(RESULT_OK, new Intent(this, MainActivity.class));
         finish();
     }

On main activity

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

But my acyncTask do not called and log not printed. How correctly I can do this? Thanks!

解决方案

Using startActivityForResult() with a PreferenceActivity is rather unusual. PreferenceActivity is designed to be used with startActivity(). If the one starting the PreferenceActivity cares about preference changes, it should register a preference change listener with the SharedPreferences object.

I recommend that you use a SharedPreferences.OnSharedPreferenceChangeListener instead. Or, simply re-read the preferences you care about in the original activity's onStart() or onResume() method.

No, that will not work. onPause() is too late to call setResult().

这篇关于如何从preference屏幕返回主活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 02:38