在创建活动之前,Oncreate等待所有方法的完成

时间:2018-04-09 01:50:36

标签: java android android-studio java-threads

I' M试图显示我的Andorid的应用的介绍,初始化此介绍的方法是从主活动的onCreate()方法调用时,做了调试当我把一个断点上显示介绍活动的方法,在我的设备中显示主要活动。

有没有办法阻止这种情况?

MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    validateFirstTime();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

private void validateFirstTime() {
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            //  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 activity has never started before...
            if (isFirstStart) {

                //  Launch app intro
                final Intent i = new Intent(MainActivity.this, IntroActivity.class);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(i);
                    }
                });

                //  Make a new preferences editor
                SharedPreferences.Editor e = getPrefs.edit();

                //  Edit preference to make it false because we don't want this to run again
                e.putBoolean("firstStart", true);

                //  Apply changes
                e.apply();
            }
        }
    });
    // Start the thread
     t.start();
}

我将断点放在validateFirstTime()方法的if语句中, 在恢复断点之前,活动已经显示在te设备中。

为什么?

1 个答案:

答案 0 :(得分:1)

这是因为validateFirstTime()内的代码运行在与以下代码不同的线程上:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

在主线程上运行。

您可以通过将代码移到线程之外来解决问题:

private void validateFirstTime() {
  SharedPreferences getPrefs = PreferenceManager
                    .getDefaultSharedPreferences(getBaseContext());

  //  Create a new boolean and preference and set it to true
  boolean isFirstStart = getPrefs.getBoolean("firstStart", true);

  //  If the activity has never started before...
  if (isFirstStart) {

    //  Launch app intro
    Intent i = new Intent(MainActivity.this, IntroActivity.class);
    startActivity(i);

    //  Make a new preferences editor
    SharedPreferences.Editor e = getPrefs.edit();

    //  Edit preference to make it false because we don't want this to run again
    e.putBoolean("firstStart", false);

    //  Apply changes
    e.apply();
  } else {
    setContentView(R.layout.activity_main);
  }
}

然后在onCreate()中你可以使用它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    validateFirstTime();
}

或者,您可以通过将所有第一次检查作为这样的方法来使用更简单的方法:

private boolean isFirstTimeUsage() {
    SharedPreferences getPrefs = PreferenceManager
                    .getDefaultSharedPreferences(getBaseContext());

  //  Create a new boolean and preference and set it to true
  boolean isFirstStart = getPrefs.getBoolean("firstStart", true);

  //  If the activity has never started before...
  if (isFirstStart) {
    //  Make a new preferences editor
    SharedPreferences.Editor e = getPrefs.edit();

    //  Edit preference to make it false because we don't want this to run again
    e.putBoolean("firstStart", false);

    //  Apply changes
    e.apply();
  }

  return isFirstStart;
}

然后你可以在onCreate中使用这样的方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean isFirstTime = isFirstTimeUsage();
    if(isFirstTime) {
      //  Launch app intro
      Intent i = new Intent(MainActivity.this, IntroActivity.class);
      startActivity(i);
    } else {
      setContentView(R.layout.activity_main);
    }
}