应用程序退出并重新启动后保存和恢复AlertDialog SetSingleChoice选项

时间:2014-09-19 08:48:38

标签: android android-actionbar

我使用SingleChoiceItemsActionBar中使用DialogBuilder。我需要在退出应用程序后保存所选项目,然后在再次访问应用程序时恢复已保存的设置。

我看到很多共享偏好的示例以及onRestoreInstanceState()onSaveInstanceState(),但我很困惑。下面的代码解释了我的所作所为。

Dialog Builder

我在 - >中保存了所选选项的当前状态selectPosition ..然后将selectedPosition保存在全局变量isChecked中并将其设置为SelectSingleChoice参数。

public void displaySortDialog(final Context context) {

    int selection = context.getSharedPreferences(PREF_NAME,
            Context.MODE_MULTI_PROCESS).getInt("Selection_key", 0);
    Toast.makeText(getApplicationContext(), "Start Sel :"+selection , Toast.LENGTH_SHORT).show();
    CharSequence[] sort_options = { "Z-A", "A-Z", "Size" };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.sort_apps));
    builder.setSingleChoiceItems(sort_options, selection,
        new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int selected_sort) {
                    /*
                     * Toast.makeText(getApplicationContext(),
                     * sort_options[selected_sort], Toast.LENGTH_SHORT)
                     * .show(); // isChecked = restoredChecked;
                     */
                }
            });

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // saving
            context.getSharedPreferences(PREF_NAME,
                    Context.MODE_MULTI_PROCESS).edit()
                    .putInt("Selection_key", id ).commit();
            Toast.makeText(getApplicationContext(), "Choosen :"+id, Toast.LENGTH_SHORT).show();
        }
    });
    builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    builder.create().show();
}

声明并使用displaySortDialog功能

public boolean onOptionsItemSelected(MenuItem item) {
    boolean result = true;
    result = menuChoice(item);

    switch (item.getItemId()) {
        case R.id.menu_Sort_By_Size: {
            displaySortDialog(getBaseContext());
            break;
        }
        case R.id.menu_Action_Search: {
            // openSearch();
            break;
        }
        default: {
            result = super.onOptionsItemSelected(item);
            break;
        }
    }

    return result;
}

当我长按主页按钮或从应用程序按主页按钮时,使用以下代码,所选设置似乎正常。它们被选中并保存为我为消息加密以确保它们被选中,这意味着onSaveInstanceState正在工作,因为显示了onSaveInstanceState中的Toast消息。但是,当我尝试恢复通过onRestoreInstanceState()保存的设置时,它无法正常工作。退出应用程序后,设置将恢复为默认值。

public void onRestoreInstanceState(Bundle savedInstanceState) {
    if(savedInstanceState != null){ 
        isChecked = savedInstanceState.getInt("SELECTED_SORT_ITEM");
        Toast.makeText(getApplicationContext(), "RESTORED: "+isChecked, Toast.LENGTH_SHORT).show();
    }    
}

public void onSaveInstanceState(Bundle savedInstanceState) {
    //outState.putInt(SELECTED_SORT_ITEM, getActionBar().getSelectedNavigationIndex());
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt(SELECTED_SORT_ITEM, isChecked);
    Toast.makeText(getApplicationContext(), SELECTED_SORT_ITEM+isChecked, Toast.LENGTH_SHORT).show();
}

当我从应用程序按主页按钮或长按主页按钮并再次选择应用程序时,会显示Toast OnSaveInstanceRestore 。但退出应用程序后,我无法恢复所选设置。

如果您能帮助我使用这些方法或了解其他一些方法,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用共享偏好

像:

    // For saving
    context.getSharedPreferences(PREF_NAME,Context.MODE_MULTI_PROCESS)
    .edit()
    .put("Selection_key",selectedPosition)
    .commit();

    //For retrieve
    int selection = context.getSharedPreferences(PREF_NAME,Context.MODE_MULTI_PROCESS)
    .getInt("Selection_key",0); // 0 being default selection value, put whatever u want

   // int selection, Use this Selection for your UI