[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1285
  • Last Modified:

postDelayed

I have a couple questions about postDelayed, see context below.

1. What is postDelayed?
2. What is it used for?
3. When should it be used? In other wods, in what circumstances should it be used?
4. What is "new Runnable()"?
5. Don't you have to declare a Runnable variable first? e.x. Runnable r = new Runnable()
6. What is public void run() if it never gets called. In other words, what's the point of creating a "run" method if it never gets called explicitly. Does it get called implicitly somewhere?

public void onClick(View v)
{
    ToggleButton.postDelayed(new Runnable()
       {
           public void run()
           {
              // do something
           }
        }
}
0
InfoTechEE
Asked:
InfoTechEE
1 Solution
 
SanthanaTechnical LeadCommented:
1. What is postDelayed? 2. What is it used for?
From the android documentation Threads
When an application is launched, the system creates a thread of execution for the application, called "main." This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events. It is also the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread.
The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread, and system calls to each component are dispatched from that thread. Consequently, methods that respond to system callbacks (such as onKeyDown() to report user actions or a lifecycle callback method) always run in the UI thread of the process.
For instance, when the user touches a button on the screen, your app's UI thread dispatches the touch event to the widget, which in turn sets its pressed state and posts an invalidate request to the event queue. The UI thread dequeues the request and notifies the widget that it should redraw itself.

PostDelay will put a task/Runnable into the UI Thread with the given delay. In android all the UI related operations will be executed in the UI thread.
By defalult this UI thread will have one handler let say UI handler.
ToggleButton.postDelayed(new Runnable()...,1000);  Here we are passing a task/Runnable into the  UI thread with 1 sec delay

For more information read this docs,,
http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html
http://developer.android.com/reference/android/os/Handler.html

3. When should it be used? In other wods, in what circumstances should it be used?In some situation while executing  Non_UI  thread we want to do some operation in the UI  thread / We want to update UI from Non_UI thread.
In some situations Non_UI thread may have the references of View. But if we try to change any UI state/UI related operation  it will throw the illegal access exception
(Some thig like this :Accessing UI thread from NonUI.....). That kind of situations we will use this postDelay()/post() to interact with UI thread..

4. What is "new Runnable()"?It is a type of Inner class in java called Anonymus inner. Here we are instantiating a anonymus class   that has implemented Runnable interface.
Basically we are creating a Runnable instance.
5. Don't you have to declare a Runnable variable first? e.x. Runnable r = new Runnable()
Yes we can
ToggleButton.postDelayed(r);

6. What is public void run() if it never gets called. In other words, what's the point of creating a "run" method if it never gets called explicitly. Does it get called implicitly somewhere?
Please go through the basic java Threading....
http://docs.oracle.com/javase/tutorial/essential/concurrency/
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now