1

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?

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Hi @Aleks G in AsyncTask do your work in the doinbackground which means your downloading works from server. then you have the onpostexecute option in AsyncTask in that method use the redirection. if you do this you wont get the black screen . and also in Activity B use Async task without loader to bind the non ui datas . – itsrajesh4uguys Jun 14 '12 at 13:21
  • @Rajesh: that's exactly what I'm doing - please re-read my question carefully. The problem is in the `onCreate` method in activity B. In emulator it takes about 5 seconds to complete - just to inflate the layout, setup local reference and do the primary initialisation. Inflating layout alone takes about 3 seconds. All these 5 seconds I'm staring at the black screen. – Aleks G Jun 14 '12 at 13:25
  • @ Aleks you are downloading the data in Activity A right. then you are passing to Activity B. Instead of that you can use Async Task in Activity B know. In oncreate just start the async task with the progress like downloading details ... then in the post execute u can bind the datas – itsrajesh4uguys Jun 14 '12 at 13:34

2 Answers2

0

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.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Ok, suppose, the activity is in foreground, but I hate the view of the black screen. Anything that can be done about it? Have it transparent or something? Anything? The problem is that the initialisation takes some time and I want this process to be "user-friendly" – Aleks G Jun 14 '12 at 13:20
  • Make yourself a full screen image and put it as fill_parent / fill_parent in your xml. Set everything except the image to be invisible. Immediately after setContentView() completes this image should be the only thing on the screen. After you are done initializing all of your other Views set them all to visible and set the image to invisible. This is actually an instance where a "Splash screen" makes sense, because you are using it only while you are doing some work, to provide a better user expereience. – FoamyGuy Jun 14 '12 at 13:24
0

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

Philippe Girolami
  • 1,876
  • 1
  • 13
  • 15