Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

runOnUiThread() method in fragment [duplicate]

Can I use runOnUiThread at fragment. And how to do it in fragment?

 MainActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this,"any mesage",Toast.LENGTH_LONG).show();
        }
    });
like image 711
yousef Avatar asked Dec 15 '22 12:12

yousef


2 Answers

try

getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getActivity(),"any mesage",Toast.LENGTH_LONG).show();
        }
    });
like image 141
Deepak Goyal Avatar answered Dec 21 '22 09:12

Deepak Goyal


Use getActivity() instead of MainActivity.this. Also use getApplicationContext() for the Toast.makeText() method

getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getActivity().getApplicationContext(),"any mesage",Toast.LENGTH_LONG).show();
        }
    });
like image 41
Nabin Avatar answered Dec 21 '22 11:12

Nabin