Question

I have the following scenario: in activity A, when a user clicks a button, the app sends a request to the server to retrieve some information, then this information is placed into an intent to start activity B.

In activity B, in onCreate method, I do setContentView(R.layout.activity_b) (this layout is quite complex, so even inflating it from XML takes a while), then do a bunch of initialisation. Finally, in onStart I do some final preparation/arrangements of the components.

In activity A, when the user presses a button, I start an AsyncTask, which gets the info from the server and starts activity B, passing the required info into intent. When I call 'startActivity', the window of activity B slides into position straight away, however it may take a few seconds to complete the initialisation. During these seconds, I see a black screen. Then, finally, the onStart is executed and everything goes on.

What I would like to do is to complete all this initialisation off-screen, before activity B slides into view, so that when it "arrives", all the layout is already present. Any ideas how to achieve this?

Was it helpful?

Solution

I don't think you can. Activities by their nature are only "alive" while they are showing on the screen.

So I think there is no way that you can signal an Activity to start doing work (i.e. instantiating Views) without it being in foreground on the screen.

I would say that your best option for better user experience is to use some kind of progress indicator that shows the user things are loading, and then is hidden once the loading is complete.

OTHER TIPS

Perhaps fragments could help ? Change Activities A & B to Fragment A & B and load fragment B in the background without showing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top