1

Currently here is what I am following, As soon as my app is launched, I have to send a request for REST service, It will take little time , so I thought of showing loading screen,

In onCreate() of my Activity , first thing will be to show loading screen(progress dialog) , And I kick off the background Activity using AsyncTask , i.e. requesting for REST service and onPostexecute() I close the dialog and then I do setContentView(myxml); and update the UI .

Can this approach be improved ?

Problem which I faced was ,

Sometimes , Garbage collector may start(due to various reasons) and my app hangs at loading screen forever , because of Garbage collector , even request for REST service is not sent and because of it some wake up call comes and rest is disaster and Force close.

But sometimes even ForceClose doesnot come fast , may be because of GC. so I cannot even go back and stuck in loading screen. Only thing which I can do at that point is to come back HOME. After that If I come back to my app its still loading , so definitely this approach seems to be a bad design. Whats the right approach ?

TheCottonSilk
  • 8,662
  • 2
  • 26
  • 37
sat
  • 40,138
  • 28
  • 93
  • 102
  • 3
    I don't think it's completely bad. I mean without a coded example it's hard to say. But it seems rather sound as long as you handle onCreate() and onDestroy() correctly for when the device has it's orientation changed. The ForceClose should not ever happen because of a GC. Make sure to set a timeout on the rest service call to make sure that the onPostExecute can actually happen and not starve out the entire threadpool. – Greg Giacovelli Feb 18 '11 at 07:12

3 Answers3

1

You could implement a splash screen like I've done here

This is basically starting an async task that is showing the splash image, doing the work in doInBackground() line 51) and afterwards forwarding to the next activity.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
1

If you are expecting your REST call to take significant time, maybe you also should consider using service instead of async task. Why? Because on every orientation change, your activity will be destroyed and new async task will be created again, so the REST call is done again from the start. On the other hand, when using service, you can issue REST call once and only trace it's status in activity (which requires you to properly handle onPause, onCreate functions).

I doubt that your problems have anything to do with garbage collector - check your stack trace after force close and try to understand what really happens. It must be that you are doing something wrong...

dstefanox
  • 2,222
  • 3
  • 19
  • 21
  • You can use an Application class and reference the AsyncTask there, then you won't have to think about orientation changes. Have a look at this question --> http://stackoverflow.com/questions/4584015/handle-screen-orientation-changes-when-there-are-asynctasks-running/4586760#4586760 – Flo Feb 18 '11 at 08:25
  • As suggested in "Google I/O 2010 - Android REST client applications", services are preferred way to go with REST apps. Of course, no one can force you to do that way, but this is the one more reason to use services. In all cases, including the one with Application class, you need to take care about orientation changes, at least to track state of your async task after orientation change, also to re-appear some kind of progress dialog and so on. Simply put, there is no magic solution, you need to take care about several things in cases like this. – dstefanox Feb 18 '11 at 11:37
  • 1
    One part if the problem I solved , It was silly , I was not able to go back from loading screen because , I had not used setCancellable to true on progress dialog and was not listening to onCancelListener, thought this is applicable only if we have buttons, So going back works proper now, About GC I will have to pin point whether the problem was really because of that or not. Thanks everyone. – sat Feb 24 '11 at 14:18
0

Have you watched the video Google I/O 2010 - Android REST client applications? on the page http://developer.android.com/videos/index.html

kharles
  • 326
  • 5
  • 14