tags:

views:

60

answers:

2

Here's my use case:
The app starts at a login screen. You enter your credentials and hit the "Login" button. Then a progress dialog appears and you wait for some stuff to download. Once the stuff has downloaded, you are taken to a new activity. Exactly which activity you are taken to depends on the server response.

Here's my problem:
If you go HOME during this login/download process, at some point in the near future your download will complete and will invoke startActivity(). So then the new activity will be pushed to the foreground, rudely interrupting the user. I can't start the activity before I start the download, because, as I mentioned earlier, the activity I start depends on the result of the download.

I would obviously not like to interrupt the user like this. One way to solve this is to refrain from calling startActivity() until the user returns to the app. I can do this by keeping track of the LoginActivity's onStop() and onRestart(). But I'm wondering, is there any way to create the activity while it is in the background? That way the user returns to the app and he is ready to go... otherwise he would have to wait for the new activity to be created (which could take some time because the new activity also has to download and display some data).

Update: Guess what? I LIED! I could have sworn that starting this activity was causing it to come to the foreground, but I went back to test it again and the problem has magically disappeared. I tested in both 1.6 and 2.0.1 and both OSes were smart enough not to bring a backgrounded task to the front.

+2  A: 

I suspect you do the download in a Service. If so, the Service should create a Notification upon completion which the user can activate himself. The PendingIntent will hold which exact Activity to start.

Of course, when the Activity is still active, no Notification should be posted, so you should mention to your Service when your Activity is active (in onResume()), and when not (in onPause()), if it's active, the service could let your activity start the correct activity.

MrSnowflake
I don't use a service, it's just a simple `AsyncTask`. And having the user start the activity from a notification amounts to the same thing as starting the activity once they return to the app. So it doesn't quite solve my problem, but having a notification might be a nice bonus, so thanks for the idea.
Neil Traft
Well, if you want the user to be able to close the activity, while still downloading, I think you should use a `Service` for the downloading!
MrSnowflake
A: 

I would look at keeping the downloading of data seperate from the Activities, and instead in a Service. That way all the data can be downloaded in the background regardless of the Activity which should be (or is) showing.

Then, when a user returns to the application, the app can choose (in onResume) which Activity to display based on whether the right data has been downloaded.

This would be preferable if the completion of the data download is not important enough for a Notification.

bdls
Thanks, but that really doesn't have anything to do with the problem at hand. This downloading is happening in a separate thread so it does not slow down the UI, and the activity being backgrounded does not affect the download. A service would be inappropriate for this use case; it's not a very long download, it's just long enough that the user might go somewhere else.
Neil Traft
I realise this didn't directly answer your question, but I think it provided a valid alternative. I think you can think of the situation as this: If you want things to happen in the background, do it in a `Service`. An `Activity` should only be relied on to do something when it is in the foreground. I don't believe you can start an `Activity` in the background. In that case, you'll have to start the new `Activity` when the user returns to the app.
bdls