1

how to run a task on ui thread. I am trying to run a task on ui thread

  MainActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, 
                        "Mytask finished", 
                        Toast.LENGTH_SHORT).show();
                }
            });
Jenz
  • 8,280
  • 7
  • 44
  • 77
Hemant Shori
  • 2,463
  • 1
  • 22
  • 20

1 Answers1

10

i found the solution for running task on ui thread in the fragment activity just change the MainActivity.this to getActivity() that will fix the error. i think this might help.

Thread timer = new Thread() {
                @Override
                public void run() {
//do something
     getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity(),
                            "Token Generated", Toast.LENGTH_SHORT).show();
                        }
                    });
       }
            };
            timer.start();
Hemant Shori
  • 2,463
  • 1
  • 22
  • 20