bitanon
bitanon

Reputation: 135

How to provide access to main activity/context to other classes that need it?

This is how I'm providing access to my main activity to all classes that require a context and/or need to call methods within my main activity class:

public interface InterfaceMainActiv {
    ActMain getMainActiv();
}

I know this is super bad form, but is it ok to just hand out access to your main activity where required? Will do so break anything? @OutOfBounds 94 Specifically I'm worried about this warning: enter image description here

Upvotes: 0

Views: 1268

Answers (3)

Chapz
Chapz

Reputation: 625

Use interfaces!

  1. Create an interface with methods that you want to call and they require context. Lets say you have interface Demo which has the method doSomething(Param a);

  2. Implement that interface in the activity that will be active ATM when you would call this method. So basically you will have MyActivity implements Demo and that activity has doSomething(Param a) method with context available.

  3. Make the constructor of the class from which you want to call the method take the Demo interface as the parameter. So for ex. public MyAdapter(Demo demo). And then when constructing the object, pass the activity implementing that interface - new MyAdapter(myActivity)

  4. Call interface method inside your class (demo.doSomething(data)), it will be executed inside the activity. Pass all the data that you need to work on through the params of the interface method.

  5. Profit???

This comes down to the basic principles of OOP.

Upvotes: 1

Ivan Milisavljevic
Ivan Milisavljevic

Reputation: 2068

You have to be very careful when you pass Context instances around your app since its very easy to cause a memory leak on Android. Best answer to your question is DO NOT DO IT, instances to android specific objects (Activity, Fragments, Application - Context) shouldn't be hold/referenced longer than their respective life-cycles.

When you declare any View to be static, your causing memory leak since static view is not tided to Activity/Fragment life-cycle (but still holding the reference to it) than rather on Class that resides in and cannot be GC-d (garbage collected) when respective Activity/Fragment completes.

In order to avoid memory leaks consider:

  • changing your code organization
  • using one of the event bus libraries like this one()
  • using plain java interfaces to communicate between components

If you have some non-android methods that require context you can always pass Application context with

 getActivity().getApplicationContext() // Application Context will live as long your app lives so you don't have to worry about memory leaks

Same applies if you want to get activity reference from your fragment: getActivity() will return Activity object which you can cast to your Activity and later use it to call appropriate methods

Upvotes: 0

OutOfBounds 94
OutOfBounds 94

Reputation: 75

public class GlobalClass extends Application {
public static GlobalClass ginstance;
public void onCreate() {
    ginstance = getApplicationContext();
}

}

and then modify your manifest file

<application
    android:name="com.you.yourapp.GlobalClass"

Upvotes: 0

Related Questions