简体   繁体   English

模式强制用户设置首选项

[英]Pattern to force user to set preferences

My application requires connection to a server, hence it will not work without set preferences. 我的应用程序需要连接到服务器,因此没有设置首选项它将无法工作。 The preferences are set using SharedPreferences . 首选项使用SharedPreferences设置。 What I am looking for is a pattern to direct the user to the settings activity. 我正在寻找的是一种指导用户进行设置活动的模式。 What is common? 什么是常见的?

Currently I am checking on onCreate and onResume whether all preferences are set, otherwise I start the settings activity. 目前我正在检查onCreateonResume是否已设置所有首选项,否则我启动设置活动。 The problem is: 问题是:

  1. the activity is started twice (through onCreate and onResume ) 活动开始两次(通过onCreateonResume
  2. the user might not know that he/she can go back to the main activity with the back hardware key 用户可能不知道他/她可以使用后备硬件密钥返回主活动

Any suggestions to implement this? 有任何建议来实现吗?

  1. onResume will always be called after onCreate , therefore you only need to perform the check and start the new activity in the onResume method. onResume将始终在onCreate之后调用,因此您只需执行检查并在onResume方法中启动新活动。

  2. You could display a Toast when the SettingsActivity loads, such as: 您可以在加载SettingsActivity时显示Toast ,例如:

     Toast.makeText(context, "Press back to return.", Toast.LENGTH_SHORT).show(); 

You may also wish to show a Toast or AlertDialog when the SettingsActivity starts telling the user that they must enter these settings before they can use the app. SettingsActivity开始告诉用户他们必须先输入这些设置才能使用该应用程序时,您可能还希望显示ToastAlertDialog

I suggest you show a Popup Dialog with a message like "set preferences first" with a button to open your Settings Activity. 我建议您显示一个弹出对话框,其中包含“先设置首选项”等消息,并带有一个用于打开“设置活动”的按钮。 The check for SharedPreferences and opening the Dialog can be done in onResume. 检查SharedPreferences并打开Dialog可以在onResume中完成。
And close the Settings Activity automatically when user finishes editing settings (and presses save-Button). 并在用户完成编辑设置时自动关闭“设置活动”(并按下“保存”按钮)。

create a LauncherActivity class where you can make the decision which Activity should start - PreferenceActivity, or MainActivity: check this advice also 创建一个LauncherActivity类,您可以在其中决定哪个Activity应该启动 - PreferenceActivity或MainActivity:同时检查建议

public class LauncherActivity extends Activity {

    SharedPreferences settings;

    public void onCreate(Bundle savedInstanceState) {

        settings = getSharedPreferences(PREFS_NAME, 0);

        if (settings.getBoolean(PREFS_REG, false)) {
            startActivity( new Intent(this, MAinActivity.class) );
            finish();
        } else {
             Intent enableBtIntent = new Intent(this, PreferenceActivity.class);
         startActivityForResult(enableBtIntent, SETTINGS);
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode == SETTINGS ) {
             startActivity( new Intent(this, MAinActivity.class) );
             Editor editor = settings.edit();
             editor.putBoolean(PREFS_REG, true);
             editor.commit();
             finish();
        }

    }
}

And if user "registered" then set the PREFS_REG to true in onActivityResult() 如果用户“注册”,则在onActivityResult()中将PREFS_REG设置为true

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

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