【发布时间】:2015-05-09 18:57:24
【问题描述】:
正如标题所说,场景是: 首次使用应用程序时,显示屏幕 A。 完成屏幕 A 后,按钮将引导您进入屏幕 B。 从现在开始,当您启动应用程序时,屏幕 B 将始终是主“屏幕”(活动?)。 我正在尝试这 2 天,但我无法得到它。 有人请解释得详细一点,或者更好地给我一个code.rar,以便我研究它。我要疯了!!!
【问题讨论】:
标签: java android xml android-activity launch
正如标题所说,场景是: 首次使用应用程序时,显示屏幕 A。 完成屏幕 A 后,按钮将引导您进入屏幕 B。 从现在开始,当您启动应用程序时,屏幕 B 将始终是主“屏幕”(活动?)。 我正在尝试这 2 天,但我无法得到它。 有人请解释得详细一点,或者更好地给我一个code.rar,以便我研究它。我要疯了!!!
【问题讨论】:
标签: java android xml android-activity launch
只需在 AndroidManifest.xml 和 Activity A onCreate() 中将您的 Activity A 声明为启动器 Activity,您就可以简单地执行此操作
private SharedPreferences mSharedPreferences;
private Editor mEditor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
mSharedPreferences = getSharedPreferences("yourPrefsFileName", Context.MODE_PRIVATE));
mEditor = mSharedPreferences.edit();
if (mSharedPreferences.getBoolean("isfirstTime", true)) {
mEditor.putBoolean("isFirstTime",false);
mEditor.apply();
}else{
startActivity(new Intent(this, ActivityB.class));
overridePendingTransition(0, 0);
finish();
}
}
【讨论】:
所有你必须像这样检查
SharedPreferences prefs = getSharedPreferences("mySHaredpref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
boolean isFirst = prefs.getBoolean("isfirstTime", true);
if(isFirst) {
Intent intent = new Intent(this, ActivtyA.class);
editor.putBoolean(KEY_IS_FIRST_TIME, false);
editor.commit();
startActivity(intent);
}
else{
Intent intent = new Intent(this, MainActivty.class);
startActivity(intent);
}
【讨论】:
public class FirstActivity extends Activity {
public void onCreate(Bundle saved){
super.onCreate();
SharedPreferences prefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
if (!prefs.getBoolean("firstStart", true)){
startActivity(this, SecondActivity.class);
finish(); // Finish the current one, is like forwarding directly to the second one
}
}
}
当您完成显示第一个活动时,只需将共享首选项布尔标志设置为 false:
prefs.getEditor.setBoolean("firstStart", false).commit();
【讨论】:
SharedPreferences sp = getSharedPreferences("checking",MODE_PRIVATE);
String data = sp.getString("check", "");
if (data.equals("success")) {
//one time proccess code
//with follow code
SharedPreferences sp= getSharedPreferences("checking",MODE_PRIVATE);
Editor e1 = sp.edit();
e1.putString("check","success");
e1.commit();
} else {
// code for main
Intent intent = new Intent(this, MainActivty.class);
startActivity(intent);
}
【讨论】: