RunOnUiThread () method in fragment

Can i use runOnUiThread

in a snippet. And how to do it in a snippet?

 MainActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this,"any mesage",Toast.LENGTH_LONG).show();
        }
    });

      

+3


source to share


2 answers


try



getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getActivity(),"any mesage",Toast.LENGTH_LONG).show();
        }
    });

      

+4


source


Use getActivity()

instead MainActivity.this

. Also use getApplicationContext () method for methodToast.makeText()



getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getActivity().getApplicationContext(),"any mesage",Toast.LENGTH_LONG).show();
        }
    });

      

+4


source







All Articles