开发者

Can I create an activity for a particular task without that task coming to the foreground?

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 co开发者_如何学JAVAuld 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜