Launcher Activity 和 Thread run() 被调用两次

时间:2021-06-20 10:05:49

标签: java android android-studio

我的应用程序启动了两次,奇怪的是,它取决于 LOAD_TIME 哪个活动被调用了两次。延迟很长,比如> 5000 毫秒,我的主要活动被调用了两次。正如您在快照中看到的那样,在 2000 毫秒内,我的启动活动和我的 run() 线程被调用了两次。

它也只发生在我的模拟器上,如果我使用我的实体手机一切正常。我认为这可能与 Consumer 类有关,它需要更新的构建版本。但我认为它需要 24,而且我使用的是 Nexus 5 和 30 版本,所以我应该没问题。

Ami 在生命周期或线程方面做错了吗?

public class StartUpActivity extends AppCompatActivity {

    public final int LOAD_TIME = 2000;
    public static int counter;
    final Handler handler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        counter++;
        Log.d("debug counter startup: ", counter+"");

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_start_up);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titel_bar);

        handler.postDelayed(new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void run() {
                Log.d("debug Handler: ", "run()");
                PreLogin checkLoginStatus = new PreLogin();
                checkLoginStatus.CheckIfStillLoggedIn((value) -> {
                    if(value==true){
                        Log.d("debug checkLogin", "checkIfStillLoggedIn=true");
                        Intent intent = new Intent(StartUpActivity.this, MainActivity.class);
                        intent.putExtra("caller", "StartUpActivity");
                        startActivity(intent);
                        finish();
                    }
                    else{
                        Log.d("debug checkLogin", "checkIfStillLoggedIn=false");
                        Intent intent = new Intent(StartUpActivity.this, ChooseLoginOrRegistrationActivity.class);
                        intent.putExtra("caller", "StartUpActivity");
                        startActivity(intent);
                        finish();
                    }
                });
                handler.removeCallbacks(null);
            }
        }, LOAD_TIME);
    }
}

AndroidManifest:

 <activity
            android:name=".StartUpActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

enter image description here

0 个答案:

没有答案
相关问题