0

我正在制作一个启动画面,它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.
}
4

1 回答 1

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();
于 2019-01-04T14:16:52.993 回答