how to show toast message in not extend activity class

后端 未结 3 1712
小蘑菇
小蘑菇 2021-01-19 08:01

hi this is my helper class where i check internal connection and xml paersing and use this class to another activity the problem is when server connected is working fine b

3条回答
  •  无人及你
    2021-01-19 08:37

    Create myToast method in your application class, Like

    public void myToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
    

    and add the following code to your class,

    private MyApplication application;
    public AgAppHelperMethods(Context context) {
      application = (MyApplication)context.getApplication();
    }
    

    Finally, call myToast method, where you want, like

    applicaion.myToast("msg you want to show");
    

    Note: replace MyApplication with your application class

    I have not tested, but this may work for u.

提交回复
热议问题