0

I need Handler in more then one Activity.

Can I make it static like

public static Handler handler = new Handler();

Or how can I use it in more then one activity? Can I have more then one handler if this one can not be static?

I was reading that Handler must be static otherwise I'll get an exception in another thread that this is not good idea.

Which way is correct?

BryanH
  • 5,826
  • 3
  • 34
  • 47
senzacionale
  • 20,448
  • 67
  • 204
  • 316

4 Answers4

1
I need Handler in more then one Activity

why then you just not create a handler per acitvity?

Or how can I use it in more then one activity?

Just create it per activity, they all should bind to same Looper object

From docs :

When you create a new Handler, it is bound to the thread / message queue of the thread that is `creating it`

Can I have more then one handler if this one can not be static?

sure

I was reading that Handler must be static otherwise I'll get an exception in another thread that this is not good idea.

the only issue that i found was this This Handler class should be static or leaks might occur: IncomingHandler but as it is mentioned, it is only when you post a long delayed message.

Community
  • 1
  • 1
AndroidGecko
  • 14,034
  • 3
  • 35
  • 49
1

A Handler is only valid for a thread that has been initialized as a Looper thread.

The UI thread is a Looper thread, and most android components run on the UI thread, for instance activities, services, content providers, custom application classes and more including any class you instantiate on the UI thread.

Instead of creating handlers you can use:

http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

but if you want to use messages instead you have to use handlers.

When the lint error comes up warning you to make your handlers static it means to use a static nested class instead so you don't get implicit references to the outer class and all its members which can introduce issues in some cases. You can declare your handlers like this to avoid that warning:

static  MyHandler extends Handler { 
    // TODO: implement message handling, etc here
}

private mHandler = new MyHandler();
Ian Warwick
  • 4,774
  • 3
  • 27
  • 27
  • Application is statically initialized on the Main thread, which is the same as the ui thread, therefore has the ui looper. Why would declaring a static handler in Application be a problem? – Amir Uval Jan 22 '13 at 11:02
  • Ok I will remove the last paragraph from my answer as your comment suggests that it's incorrect which now thinking about it I agree with – Ian Warwick Jan 22 '13 at 13:30
0

If your Handler implementation is thread-safe or your program is running in a single threaded environment, then you can have a single handler instance which will process multiple events/activities one after another and hence declare it as static.

If it is not thread-safe, then you would be seeing unexpected results if you use the same handler instance for multiple activities/events in a multi-threaded environment.

Jiji
  • 383
  • 1
  • 7
  • it is not thread safe. And yes I am using more threads not just one. So you suggest to use different handler in every class? – senzacionale Dec 03 '12 at 21:10
  • so like it said here. One handler for one activity, service, ... ?http://www.vogella.com/articles/AndroidPerformance/article.html#concurrency_handler – senzacionale Dec 03 '12 at 21:13
0

Handlers are always to be static. You have to create Handlers in onCreate of Activity. Upto my knowledge you have to create separate handlers for separate activity.

android.fryo
  • 1,469
  • 1
  • 14
  • 18