Passing Activity or Context to other instance

后端 未结 5 1798
渐次进展
渐次进展 2020-12-19 05:36

Can you please tell me whats the best practice to pass activity or context to other instance(;)

But...,

  • What is better to pas
5条回答
  •  执笔经年
    2020-12-19 06:01

    Create Object of The Helper class from your Activity and pass 'this' as the context, say

    MyHelperclass helper=new MyHelperclass(this);
    

    In the Helper class Get this Context via its constructor

    Context context;
    MyHelperClass(Context context){
    this.context=context;
    }
    

    Now You can pass this context to the makeText() method of Toast class.

提交回复
热议问题