Passing Activity or Context to other instance

后端 未结 5 1814
渐次进展
渐次进展 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:12

    To use context from another instance, you can create static variable then refer to it in instance you wish to use

    public class MyClassA(){
         public static Context context;
    
         private void setcontext()
         {
             context = MyClassA.this;
         }
    
    }
    
    public class MyClassB(){
         private Context classA_context = MyClassA.context;    
    }
    

提交回复
热议问题