简体   繁体   English

如何处理子活动返回的状态

[英]How to handle the status of the returning back from a child activity

I want after directing the user to the preferences window "setting", to have an indicator implies whether or not the user returned back from the preferences activity whether he ticked a designated option or not. 我希望在将用户定向到首选项窗口“设置”之后,使指示器暗示用户是否从首选项活动中返回,无论他是否勾选了指定的选项。

I know that I can start the preferences activity" child activity " from my current activity " Parent ", using startActivity() or startActivityForResult() . 我知道我可以使用startActivity()startActivityForResult()从当前活动“ Parent ”启动首选项活动“ child activity ”。 but in this case I want to check the status of the returning back from the child activity 但是在这种情况下,我想检查child activity返回的状态

UPDATE 更新

Or to be more specific, I want to check the if the "gps enable" option which resides in the "Location Services" menu is ticked or not. 更具体地说,我想检查是否勾选了“定位服务”菜单中的“ gps enable”选项。

Let's assume you have a CheckboxPreference in your PreferenceActivity declared as: 假设您在PreferenceActivity有一个CheckboxPreference声明为:

<CheckBoxPreference
    android:defaultValue="false"
    android:key="prefAutoLogin"
    android:summary="@string/pref_autologin_summary"
    android:title="@string/pref_autologin" />

In your main activity's onOptionsItemSelected you check which menu item was pressed, and in case of the settings menu, you start your PreferenceActivity : 在主活动的onOptionsItemSelected ,检查按下了哪个菜单项,如果是设置菜单,则启动PreferenceActivity

final Intent i = new Intent(this, MySettingActivity.class);
startActivityForResult(i, RESULT_SETTINGS); 
// RESULT_SETTINGS is an integer constant to identify the preference activity

You start the MySettingsActivity for result, meaning the current activity will be notified when it returns in the onActivityResult method. 您为结果启动MySettingsActivity ,这意味着当前活动在onActivityResult方法中返回时将得到通知。 You overwrite that method to check for the modifications: 您覆盖该方法以检查修改:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) 
    {
    case RESULT_SETTINGS:
        // here you check whether the auto-login preference was checked or not:
        final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
        final boolean autoLoginChecked = prefs.getBoolean("prefAutoLogin", false);
        // prefAutoLogin is the key of your CheckBoxPreference declared in the xml above.
        if (autoLoginChecked)
        {
            // perform the actions necessary when the checkbox is ticked
        }
        else 
        {
            // ...
        }
        break;
    }
    [...]
    default {...} 
}

If you need to perform some actions only when the checkbox was ticked from false to true (and not every time it returns from the pref. activity with a checked state), you should store its state prior to start the preference activity, and match against the new state on return: 如果仅在复选框从false勾选为true时才需要执行某些操作(而不是每次从具有选中状态的pref。活动返回时),都应在启动首选项活动之前存储其状态,并与之匹配新的退货状态:

private boolean initiallyChecked; 

and before you start the settings activity: 在开始设置活动之前:

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
initiallyChecked = prefs.getBoolean("prefAutoLogin", false);

Then when the activity returns back, you don't just check if the checkbox value is true, but check to see whether it was false before: 然后,当活动返回时,您不仅要检查复选框的值是否为true,还要在之前检查它是否为false:

if (autoLoginChecked && !initiallyChecked)
{ .... }

For more reference see this post on viralpatel . 有关更多参考,请参阅有关viralpatel的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM