启动画面确定,启动时加载哪个活动?

Studio2bDesigns

我正在制作一个启动画面,它Activity根据应用程序是否是第一次(或不)启动来确定要加载的内容

代码在它自己的 Activity - 中运行MainActivity,它将充当启动画面。如果是第一次启动,我加载IntroActivity.. 如果之前启动过,我加载PrimaryActivity.

我有几个问题

1) - 是否使用runOnUiThread正确的方法来做到这一点?

2) - 我在 StackOverflow 上研究了与启动画面相关的主题,建议使用 a 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.
}
让代码

这是最好的方法。获取共享首选项以查看其用户是否第一次。如果是,将它们带到第一次活动,否则,将它们带到主要活动。

如果用户删除了应用程序并重新安装,他们将被视为再次使用的用户,因为此信息存储在本地设备上。如果您希望基于此用户,请实现一个数据库以按用户 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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章