我正在创建一个启动屏幕,它根据是否是第一次启动该应用程序来确定要加载哪个Activity
。。
代码在自己的活动-MainActivity
中运行,它将作为“启动”屏幕。如果是首次启动,则加载IntroActivity
。如果是之前启动,则加载PrimaryActivity
。
我有几个问题:
1)-使用runOnUiThread
是正确的方法吗?
2)-我在StackOverflow上研究了与启动屏幕有关的主题,建议使用Handler
-在我的特定中推荐这样做用例?
3)-确定要加载的活动后,是否应该关闭此Thread
?如果这样,该如何进行?
奖金:
4)-我打算最终使该活动成为弹出式加载窗口。
最简单的方法是什么?
在此先感谢您提供的任何帮助!
我当前的代码如下所示:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Make a Toast pop-up.
Toast.makeText(MainActivity.this, "Checking Settings...", Toast.LENGTH_LONG).show();
//// BEGIN PREFERENCES CHECK ////
// Set the wait time for the Splash screen.
final int SPLASH_WAIT_TIME = 5000;
// Start new Thread to check for first start and load appropriate Activity.
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// Wait before continuing.
try {
Thread.sleep(SPLASH_WAIT_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Initialize SharedPreferences.
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
// Create a new boolean and preference and set it to true.
boolean isFirstStart = getPrefs.getBoolean("firstStart", true);
// If the App has NEVER started before...
if (isFirstStart) {
// Declare an Intent for loading IntroActivity.
final Intent intentLoadIntro = new Intent(MainActivity.this, IntroActivity.class);
// Launch IntroActivity.
runOnUiThread(new Runnable() {
@Override public void run() {
startActivity(intentLoadIntro);
}
});
// Make a new Preferences Editor.
SharedPreferences.Editor prefsEditor = getPrefs.edit();
// Edit Preference to make firstStart False so Intro never loads again.
prefsEditor.putBoolean("firstStart", false);
// Apply the changes.
prefsEditor.apply();
// Close MainActivity so the Back hardware button doesn't return to it.
finish();
}
// If the App HAS been started before...
else {
// Declare an Intent for loading PrimaryActivity.
final Intent intentLoadPrimary = new Intent (MainActivity.this, PrimaryActivity.class);
// Launch PrimaryActivity.
runOnUiThread(new Runnable() {
@Override public void run() {
startActivity(intentLoadPrimary);
}
});
// Close MainActivity so the Back hardware button doesn't return to it.
finish();
}
}
});
// Start Thread t to determine Activity to load after Splash (MainActivity).
t.start();
// END of onCreate.
}
// End of MainActivity.
}
答案 0 :(得分:0)
这是最好的方法。获取共享的首选项,以查看其是否是首次用户。如果是这样,将其带入第一次活动,否则,将其带入主要活动。
如果用户删除了该应用程序并重新安装,则由于此信息存储在本地设备上,因此它们再次被视为首次用户。如果您希望基于此用户,请实现一个数据库,以按用户ID存储这些标记。但是,流程将相似。
在您的Splash活动的onCreate中
// Initialize SharedPreferences.
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
// Create a new boolean and preference and set it to true.
boolean isFirstStart = getPrefs.getBoolean("firstStart", true);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if (isFirstTime) { //first time user is here.
Intent intent = new Intent(Splash.this, FirstTime.class);
startActivity(intent);
finish();
} else { //user has been here before.
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
finish();
}
}
}, 500); //half second
在您的首次FirstTime活动中,一旦用户完成了您希望他们做的任何事情,您就将更新共享的首选项并将其返回到初始屏幕以进行验证。
// Make a new Preferences Editor.
SharedPreferences.Editor prefsEditor = getPrefs.edit();
// Edit Preference to make firstStart False so Intro never loads again.
prefsEditor.putBoolean("firstStart", false);
// Apply the changes.
prefsEditor.apply();
// Go back to Splash...
Intent intent = new Intent(FirstTime.this, Splash.class);
startActivity(intent);
finish();