简体   繁体   中英

Why is onCreate being called when I press the back button?

I haven't touched development on Android in over a year, and I wasn't 100% comfortable with it previously.

However, I'm working on an app and I have three activities--A, B, and C. A is basically working as a splash screen and calls B to initialize a database with filler data, and then A calls C, which is the home screen. So the activity flow is as follows (simple enough):

A -> B, then C

My problem is that my database isn't being initialized until I back out of activity C, which basically just ends the entire app. I noticed that onCreate for B is called by then, and I can't figure out why it's being called at that time instead of when A starts activity B.

This is what I have so far. Also, I'm using this tutorial for my database, for the most part. Not sure if it makes a difference, but for reference: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Activity A (SplashScreen)

public void onCreate(Bundle saveInstanceState) { 
    super.onCreate(saveInstanceState);
    setContentView(R.layout.splash_layout);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    mHandler = new Handler();
    mRunnable = new Runnable() {
        public void run() {
            // Initialize the database on startup
            Intent initDB = new Intent(SplashScreen.this, DBInit.class);
            SplashScreen.this.startActivity(initDB);

            // Then load up the main activity
            Intent mainIntent = new Intent(SplashScreen.this, MainActivity.class);
            SplashScreen.this.startActivity(mainIntent);
            SplashScreen.this.finish();
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        }
    };
    mHandler.postDelayed(mRunnable, 2000);  
}

Activity B (DBInit)

public void onCreate(Bundle savedInstanceState) {
    // Database initialization stuff
    DBInit.this.finish();
}

I figured out that using AsyncTask is the easier and cleaner way to go. Used this SO question/topic for the resolution: How to show splash screen while initializing database?

I would go ahead and post my solution, but it's a mess of a code right now and I've got a different problem I need to resolve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM