简体   繁体   中英

oncreate is getting called when application comes to foreground

I have SplashScreen Activity and MainActivity. Once SplashScreen timeouts I am calling

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
    startActivity(intent);
    finish();

It is taking me to the onCreate of Mainactivity. Now I press device home button and keeps the app in backgroung for long time and when I am bringing the app to the foreground by clicking on Launcher icon it is taking me to the onCreate of MainActivity. Here I am doubting if device is killing my app from Application backstack then it should start from SplashScreen Activity instead of MainActivity. I have not set any launch mode tag in Manifest.xml. could you please help me

Now I press device home button and keeps the app in backgroung for long time and when I am bringing the app to the foreground by clicking on Launcher icon it is taking me to the onCreate of MainActivity.

Yes, that is the expected behavior. If the app has been in the background long enough that the OS has killed your app's process, your MainActivity will need to go through its full creation procedure, including onCreate() (though you should see that the savedInstanceState Bundle is non-null during this re-creation).

Here I am doubting if device is killing my app from Application backstack then it should start from SplashScreen Activity instead of MainActivity.

I think you misunderstand what the OS killing your app's process means. It doesn't mean that your app has been "quit", and it doesn't mean that your app will start over from scratch just like when it is launched for the first time. In fact, it is explicitly not supposed to appear as though it's starting for the first time. The main idea is that the OS killing your app's process shouldn't be something the user ever realizes happened; they should start right where they left off.

Set the intent filter in your manifest.xml in your SplashActivity as Below

<activity
    android:name=".activity.SplashActivity"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="adjustPan">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

Now if your app will first search for the Splash Screen at App Start Up .Also remember that , When you are calling - finish() ; on a activity that activity is removed from the Stack.So When you come back to the App Again you will able to see your HomeActivity .

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