Leeeeeeelo
Leeeeeeelo

Reputation: 4383

Android - Start a new activity while the application is in the background

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 ?

  1. Start a new activity directly from the onPostExecute method of the AsyncTask class.
  2. 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 ?

Upvotes: 2

Views: 1351

Answers (3)

kaushal trivedi
kaushal trivedi

Reputation: 3443

I think that it is dependent upon the OS of android. It has defined some built in priority model for each of the components.

Look at the answer given by commonsware.

change process priority in android

this gives brief idea about components priority.

Upvotes: 0

avlacatus
avlacatus

Reputation: 137

I faced a similar situation: my application went to background and after some time the app started an intent displaying another activity. However, the app's behavior now depends on the os that it's running:

  • on pre 4.4 devices the app silently opens the new activity and remains in background. When the user resumes the application, he is prompted with the second activity already.

  • on 4.4 devices (tested on 4.4.2 and 4.4.4) the app opens the second activity, but after 3-4 seconds, the app pops to foreground, interrumping the user.

Hope it helps anybody. I'm still looking solutions for the second case in order to prevent the app from popping to foreground.

Upvotes: 2

vinothp
vinothp

Reputation: 10059

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.

Upvotes: 1

Related Questions