-1

Here is my code:

public class MainActivity extends AppCompatActivity {

    Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        runnable=new Runnable() {
            @Override
            public void run() {
                Log.i("hello","runnable") ;
            }
        };
    }

}

Why is it not printing log? Do I need a handler and then pass runnable object in it? Does run() method runs only once?

rekire
  • 47,260
  • 30
  • 167
  • 264
  • You didn't start it. Call new Thread(runnable).start(); – PEHLAJ Apr 27 '16 at 06:25
  • Your question is not complete. please do it first – M D Apr 27 '16 at 06:25
  • @P.Rai `Runnable` is just an interface: you cannot start an interface – pskink Apr 27 '16 at 06:27
  • @pskink, the question is not about the interface. The question is about an _anonymous inner class_ that `implements` the interface. The OP has shown us code that creates a new instance of an anonymous class that implements `Runnable`, but the OP has not shown us any code that _does_ anything with that object. (i.e., where is the code that calls the `run()` method?, or where is the code that hands the new Runnable object off to some other object that will call the `run()` method?) – Solomon Slow Apr 27 '16 at 13:19
  • @jameslarge ok so it is `java.lang.Object` which has one additional method taken from `Runnable` iface, still you cannot "start" it – pskink Apr 27 '16 at 14:03

1 Answers1

3

Currently you just define your runnable, but it will be never called. There are some ways how you can use a runnable e.g. in a thread or also in a Handler.

Here is an example for a Thread where you cannot update the UI:

new Thread(runnable).start();

If you need to update the UI you should use a Handler like this:

new Handler().post(runnable); // do as soon as possible
new Handler().postDelayed(runnable, 300); // do it after 300ms

Normally it doesn't make sense but you can use your runnable also as a kind of callback like this:

runnable.run();

A runnable can been used multiple times by using that three examples above multiple times.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • why the 3rd way doesn't make sense? this is a normal way how `Runnable` interface is accessed (both 1st and 2nd options do that). – pskink Apr 27 '16 at 06:33
  • A runnable is normally used for Threads or Handlers. This will work of cause, but normally you wouldn't do this. The last call is done in the Handler or the Thread, in other words that call is used in frameworks and normally not in user code. – rekire Apr 27 '16 at 06:34
  • Re, "Normally it doesn't make sense but..." Using a `Runnable` as a callback makes a _lot_ of sense. But, what _makes_ it a callback is that the `runnable.run()` call happens in some _other_ method (probably in some other class) from the one that created the Runnable object. – Solomon Slow Apr 27 '16 at 13:22
  • IMHO it does not make sense to use Runnable as a callback. Better create a custom interface where it is much more clear what that interface call does. You could use a Runnable instead of View.OnClickListener if you ignore the args for a moment. However I guess you will agree with me that using an interface which tells you what is does is better. It is also more helpful if you want to have multiple callbacks. – rekire Apr 27 '16 at 13:33