Passing Activity or Context to other instance

纵饮孤独 提交于 2019-12-18 05:57:21

问题


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

But...,

  • What is better to pass? Activity or Context
  • Is good idea to have the Activity as Global (public static Activity activity)

here is my code. What would you change? (based on good android practices)


回答1:


Passing an Activity into another object that does not specifically require the Activity object is usually a bad idea. Activity extends Context so it works to satisfy a method that requires Context.

In your case, you can get the Context from the View that is passed into the method. However, if you need the Context for other purposes, you should avoid passing Activity and do something like Activity.getApplicationContext().

The reason for this is if the object you pass an Activity into lives longer than the Activity, the reference to the Activity will prevent Android from doing proper Garbage Collection (GC) and consume unnecessary memory. This is called a "memory leak".

EDIT:

To handle situations where you need to call Activity.findViewById() keep these things in mind. First, that is a View method (http://developer.android.com/reference/android/view/View.html#findViewById(int)) so be sure to call it from the correct view.

Second, if you need a View from an Activity you should pass it in as a WeakReference




回答2:


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.




回答3:


Context is an entity which provides global information about application or activity. You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class). Context is tied with lifecycle of its activity/application and commonly used for creating new objects, accessing resources e.t.c.

In your code you can use Context as a first parameter in Toast.makeText . It's also common to have global Context object for a class. You may read some explanation about Context usage here

You may need Activity object for example if you need to start new activity and create Intent object.




回答4:


Create a new Interface and implement in your Activity or Service or Intentservice class and return context as a callback

public interface ContextWraperSolution {
    Context getContext();
}

Pass this interface as a constructor parameter to non-Activity class and use it like

insanaceContextWraper.getContext()



回答5:


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;    
}


来源:https://stackoverflow.com/questions/33162348/passing-activity-or-context-to-other-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!