Issue with Threads and onCreate()

by mobilek...@googlemail.com » Wed, 11 Mar 2009 18:13:17 GMT


Sponsored Links
 Hi,

I've faced the following issue. Upon loading my app I have several
threads consuming an http output stream to update some state. I've
implemented the Handler technique and have my threads posting back
when they're ready to do so. This usually takes 2-3 seconds. The issue
comes if the keyboard is opened within this period since G1 restarts
the onCreate(), and basically repeats the process. So when the first
threads are ready to post back they're throwing NullPointerException.
The only solution that comes to my mind is to detect when the device
is about to force change of portrait to landscape and vice versa, and
at this point to stop all threads previously created so my client
won't throw the NullPointerException erros. Is this a well know issue
with a well known solution? Hope I will get some advice as this
problem is ruining my whole app.

Thanks!
--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Stoyan Damov » Wed, 11 Mar 2009 19:23:29 GMT


 It's not an issue - restarting your activity when the configuration
changes is by design.
You can mark your activity in the manifest file as one which deals
with configuration changes (hiddenkeyboard, etc.) itself, handle the
configuration change yourself and it won't be restarted.

On Wed, Mar 11, 2009 at 12:13 PM, mobilek...@googlemail.com



--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Issue with Threads and onCreate()

by mobilekid » Wed, 11 Mar 2009 19:39:54 GMT


 Thanks Stoyan,

Is it a case of declaring the activity with android:configChanges in
the AndroidManifest.xml, and then implementing what to reload in the
onConfigurationChanged(Configuration) of that particular activity?





--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Stoyan Damov » Wed, 11 Mar 2009 19:44:59 GMT


 Yes, basically if you declare your activity's orientation to be
"sensor" there's nothing else to do (I think).





--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by mobilekid » Wed, 11 Mar 2009 20:34:28 GMT


 For some reason that seems not to work in my case. I've declared the
activity as android:configChanges="orientation" in the
AndroidManifest.xml, which I assume will call onConfigurationChanged
(Configuration). So for testing purposes I've simply implemented it as
follows:

@Override
public void onConfigurationChanged (Configuration newConfig){
        Log.i(this.toString(), "I've been called!");
        super.onConfigurationChanged(newConfig);
}

However, it never gets called. Am I doing anything wrong here?!
Thanks!







--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Stoyan Damov » Wed, 11 Mar 2009 20:38:14 GMT


 keyboardHidden|orientation





--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Marco Nelissen » Wed, 11 Mar 2009 20:51:23 GMT


 That will solve the problem when opening/closing the keyboard, but
won't solve it when e.g. starting the app and then immediately hitting
the back button to exit it.







--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by mobilekid » Wed, 11 Mar 2009 21:06:16 GMT


 Sweet! Thank you!




--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Stoyan Damov » Wed, 11 Mar 2009 22:18:25 GMT


 unless he traps the back button in onKeyDown







--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Streets Of Boston » Wed, 11 Mar 2009 22:33:39 GMT


 I've been dealing with the same issues and i tried all these. And
nothing seems to work well.
First of all, having a thread that's part of your activity running
after your activity's onDestroy has been called is tricky. It can lead
to memory-leaks, etc.

I finally decided to create a Service for these kinds of tasks and it
simplified everything. It's an initial expense writing a Service, but
it pays off in your activity's code and its life-cycle handling.

Your activity binds to the service on the onCreate and unbinds it on
the onDestroy (or, even better, on onResume and onPause).
To prevent the Service from shutting down, call startService from your
activity as well, just after binding to it. When your activity
unbinds, the service won't be shut down and your service's threads can
keep running safely. When your activity is recreated, just re-bind to
the service, have your activity query the service for the most current
state of your Service's threads and voila!

Your Activity:
  Register a callback service. The service can call your activity back
with important updates:
    Handle callbacks from service.

  onCreate:
     bind to service.
     start-service as well. This will keep the service alive when
unbinding from it.
     request callback from service giving the latest state/info of the
service's threads/process.

  onDestroy:
     unbind from service.







--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Marco Nelissen » Thu, 12 Mar 2009 02:10:26 GMT


 Sure, but then what if right as the activity is starting, something
else happens that causes it to be destroyed? (incoming phone call,
camera app is  started, it could be anything, really)
My point was: instead of putting in all these hacks to try and prevent
your activity from being stopped and restarted at an inconvenient
time, it's ultimately easier to do the right thing. Like cleaning up
the threads in onDestroy, using a service instead of a thread, or
keeping the threads across activities.










--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Dianne Hackborn » Thu, 12 Mar 2009 03:54:08 GMT


 es, a Service is what you want to use if you want to continue doing work
after the user has exiting your app.

The original poster I think is dealing with thread that are just supposed to
be running while the app is being used, in which case I would avoid using a
Service, since that is likely to introduce other more subtle problems where
your app continues needing to be running even when the user has left it,
consuming memory and cpu.

I think the best way to deal with this is to introduce a static Handler
subclass for receiving messages from the threads, which has a pointer to the
current activity. Return that object in onRetainNonConfigurationInstance()
and set its activity pointer to 0; when the next activity enters onCreate(),
retrieve the object with getLastNonConfigurationInstance() and point it to
the new activity. You are guaranteed no messages will be dispatched on the
main thread between onRetainNonConfigurationInstance() and the next
activity's onCreate() call.

On Wed, Mar 11, 2009 at 7:33 AM, Streets Of Boston
<flyingdutc...@gmail.com>wrote:



--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support. All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Timo Bruck » Thu, 12 Mar 2009 06:27:03 GMT


 Dianne... instead of making a Handler subclass, would it be just as
effective to have the Handler be a field of a custom Thread class
which is reset by a dying activity instance in
onRetainNonConfigurationInstance() and set by the new activity
instance in onCreate()?

Instead of retaining the handler in
onRetainNonConfigurationInstance(), I'd just retain a pointer to the
thread.

-Timo

--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Timo Bruck » Thu, 12 Mar 2009 07:13:57 GMT


 Answering my own question, I think the answer is that Dianne's way is
better than mine.

With Dianne's way, if the thread completes while the Activity is being
destroyed/created, there's still a Handler around to post a message
to. When the new Activity is created, it'll get the message that was
queued.

With my way, if the thread completes while the Activity is in the
destroy/create state, it has no Handler to post to, and the message
will never get sent.

-Timo

--~--~---------~--~----~------------~-------~--~----~



Issue with Threads and onCreate()

by Dianne Hackborn » Thu, 12 Mar 2009 07:50:04 GMT


 Yep, it's easier to take advantage of Handler to deal with a lot of the
threading grunginess for you.






-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Exception locking surface - lockCanvas() returns a null?

If you want to use the standard 2d APIs, don't use OpenGL.








-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--~--~---------~--~----~------------~-------~--~----~

2. require eclipse 3.1 , 3.2

hi group
 i want  eclipse 3.1 , 3.2 (europa )for developing application in Android.
i searched it but i didnt get
if any one has its related links  pls give me
i m waitng for reply
its urgent !!!!!!!!!!!!!!


-- 
p...@$#@NT

--~--~---------~--~----~------------~-------~--~----~

3. How to: (new) HTTP API

4. Exception locking surface - lockCanvas() returns a null?

5. Alias custom view path in XML

6. Custom View Subclass - How do I properly specify xmlns in my layout.xml file?

7. How to extend an ArrayAdapter?