启动画面确定,启动时加载哪个 Activity?
Splash screen determining, which Activity to load on startup?
我正在制作启动画面,它根据应用程序是否是首次启动(或不是) 来确定加载哪个 Activity
。 .
代码是 运行 在它自己的 Activity - MainActivity
中,它将作为初始屏幕。如果是第一次启动,我加载IntroActivity
。如果它在之前启动过,我加载PrimaryActivity
。
我有几个问题:
1) - 使用 runOnUiThread
是正确的方法吗?
2) - 我在 Whosebug 上研究了与启动画面相关的主题,建议使用 Handler
- 这在我的 [=41 中推荐吗=]具体用例?
3) - 我是否应该在确定加载哪个 Activity 后关闭此 Thread
,如果是这样,我应该怎么做这个?
奖金:
4) - 我打算最终使这个 Activity 成为弹出式加载 window..
实现这个最简单的方法是什么?
在此先感谢您提供的任何帮助!
我目前的代码如下所示:
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.
}
这是最好的方法。获取共享首选项以查看其用户是否是第一次。如果是,将它们带到第一时间 activity,否则,将它们带到主要 activity.
如果用户删除应用程序并重新安装,他们将再次被视为首次用户,因为此信息存储在本地设备上。如果您希望基于此用户,请实施一个数据库以按用户 ID 存储这些标签。但是,流程将是相似的。
在您的 Splash 的 onCreate 中 activity
// 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
在你的第一次 activity 中,一旦用户完成了你想让他们做的任何事情,你就会更新你的共享首选项并将他们移回初始屏幕以进行验证。
// 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();
我正在制作启动画面,它根据应用程序是否是首次启动(或不是) 来确定加载哪个 Activity
。 .
代码是 运行 在它自己的 Activity - MainActivity
中,它将作为初始屏幕。如果是第一次启动,我加载IntroActivity
。如果它在之前启动过,我加载PrimaryActivity
。
我有几个问题:
1) - 使用 runOnUiThread
是正确的方法吗?
2) - 我在 Whosebug 上研究了与启动画面相关的主题,建议使用 Handler
- 这在我的 [=41 中推荐吗=]具体用例?
3) - 我是否应该在确定加载哪个 Activity 后关闭此 Thread
,如果是这样,我应该怎么做这个?
奖金:
4) - 我打算最终使这个 Activity 成为弹出式加载 window..
实现这个最简单的方法是什么?
在此先感谢您提供的任何帮助!
我目前的代码如下所示:
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.
}
这是最好的方法。获取共享首选项以查看其用户是否是第一次。如果是,将它们带到第一时间 activity,否则,将它们带到主要 activity.
如果用户删除应用程序并重新安装,他们将再次被视为首次用户,因为此信息存储在本地设备上。如果您希望基于此用户,请实施一个数据库以按用户 ID 存储这些标签。但是,流程将是相似的。
在您的 Splash 的 onCreate 中 activity
// 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
在你的第一次 activity 中,一旦用户完成了你想让他们做的任何事情,你就会更新你的共享首选项并将他们移回初始屏幕以进行验证。
// 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();