我正在制作一个启动画面,它Activity
根据应用程序是否第一次启动(或不启动)来确定要加载的内容..
代码在它自己的 Activity - 中运行MainActivity
,它将充当启动屏幕。如果是第一次启动,我加载IntroActivity
..如果之前启动过,我加载PrimaryActivity
。
我有几个问题:
1) - 是否使用runOnUiThread
正确的方法来做到这一点?
2) - 我在 StackOverflow 上研究了与启动屏幕相关的主题,建议使用Handler
- 在我的特定用例中是否推荐?
3) - 一旦我确定要加载哪个活动,我是否应该关闭Thread
它,如果是这样,我应该怎么做?
奖金:
4) - 我打算最终使这个 Activity 成为一个弹出式加载窗口..
实现这一目标的最简单方法是什么?
提前感谢您提供的任何帮助!
我当前的代码如下所示:
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.
}