0
 ((Activity)getApplicationContext()).runOnUiThread(new Runnable() {
        @Override
        public void run() {

        }
    });

This code is right? I get a trouble to think it!

zys
  • 1,306
  • 3
  • 18
  • 37

2 Answers2

1

See this answer: https://stackoverflow.com/a/6760019/923441

You should definitely NOT cast getApplicationContext() to Actvity, it is not guaranteed to work and such programming will lead to crashes down the line.

Community
  • 1
  • 1
A. Steenbergen
  • 3,360
  • 4
  • 34
  • 51
1

runOnUiThread() it's a method from Activity so if you are on an Activity you can avoid this : ((Activity)getApplicationContext()), but if you are on a Fragment, you'll need to get the first your Activity and then call this method, otherwise you won't be able to call it.

You should change this : ((Activity)getApplicationContext())

to this :

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {

    }
});

It can throw you a ClassCastException as Blackbelt said in comments.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148