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(); } }); 
0

1 Answer 1

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(); 
Sign up to request clarification or add additional context in comments.

3 Comments

sir i have searched it. i was working on something and couldnt find the answer. so thought i should share it.
@Metehan It is perfectly acceptable to ask a question and then answer it yourself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.