[英]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. 目前我正在检查
onCreate
和onResume
是否已设置所有首选项,否则我启动设置活动。 The problem is: 问题是:
onCreate
and onResume
) onCreate
和onResume
) Any suggestions to implement this? 有任何建议来实现吗?
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
方法中启动新活动。
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
开始告诉用户他们必须先输入这些设置才能使用该应用程序时,您可能还希望显示Toast
或AlertDialog
。
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.