The first activity of my application is a splash screen, where some animation is displayed while some loading occurs in a background thread using AsyncTask.
Once the loading in the background is done, I want to start a new activity. What is the correct way to do that ?
- Start a new activity directly from the onPostExecute method of the AsyncTask class.
- Check if the current activity is displayed before starting the new activity :
- If the current activity is display, start the new activity.
- If the current activity is NOT display, use a flag (boolean) and check the flag during the onResume method in order to start the new activity there.
My main concern is :
If my application went to the background (due to an incoming phone call, a home key press, …) and the background thread (AsyncTask) finished executing, and a new activity is started from the onPostExecute method while my application is still in the background : What happens ?
- Will the new activity start directly as soon as my application is visible again ?
From my experience i am answering your question
Question1
If you using AsyncTask you have to start new activity in
OnPostExecute(). In my experience this is the correct way of doing it.Question2
When ever your press the home key or receiving phone call. Your activity will go in to background until you exit the app by pressing back button(at that time your app is exited). So when you come back to your app your new activity will be visible if background process get finished. Otherwise you will see the start splash screen.