How to use a class with a context argument in a static context without causing a memory leak?

Darcar90

In my project I have this situation. This is a part of my MainActivity, I have initialized the object helperClass with the applicationContext and made it static because I have to use it in the OnClickListener of a button:

public class MainActivity {
    public static HelperClass helperClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        helperClass = new HelperClass(getApplicationContext());

        {...}

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                helperClass.doSomething();
            }
        });
    }
}

This is a part of the helper class, I need the context because I have to store some data in the app memory:

public class HelperClass{
    private Context context;

    public HelperClass(Context context) {
        this.context = context;
    }

    public void doSomething() {
        File file = new File(context.getFilesDir(), "name");
        {...}
    }
}

Now, this code works, but when I declare helperClass static there is a warning saying this is a memory leak; I tried to remove the context from the HelperClass but I could not retrieve the context from the class; I also tried to remove the static declaration but I need it because the OnClickListener is a static context.

I read here that I can use ApplicationContext, it is ok but it's an old article.

Is there a better solution?

Kelevandos

You do not need to make a field static in order to use it in the Listener. Moreover, you shouldn't, as you correctly said that it may lead to a memory leak :-) Just remove the static keyword, run the code and it will work ^^

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android inner classes memory leak and leak by context?

From Dev

How to replace one dynamic array with another without causing memory leak?

From Dev

How to use Shared Preferences in MVP without Dagger and not causing Presenter to be Context dependent?

From Dev

How to access the .class object in java without using the class name from a static context

From Dev

How to avoid memory leak in context.getSystemService(Context.CAMERA_SERVICE)?

From Java

Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)

From Dev

"Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"

From Dev

Using $this when not in object context without the use of static methods

From Dev

Passing @Context argument to method in class

From Dev

Passing @Context argument to method in class

From Dev

Will passing context to helper class in android activity leak?

From Dev

How to use a std::mutex in a class context

From Dev

Why/How is my code causing a memory leak?

From Dev

Getting Context from other class causing NPE

From Dev

cannot use this in a static context activity

From Java

How to pass context or any parameter in a static class in flutter?

From Dev

How to pass Context into public class with public static String method

From Dev

How to get context of class

From Dev

Android context memory leak via activity private member

From Dev

Possible to free Context from Singleton in Android to prevent memory leak

From Dev

Possible to free Context from Singleton in Android to prevent memory leak

From Dev

why is std::string causing a memory leak in a class even after deleting

From Dev

How to access a file in a static context?

From Dev

How to Wait a Thread in a Static Context?

From Dev

how to use the context in the tests?

From Dev

How to use context in a function?

From Dev

How to use 'this' context in middleware

From Dev

how to use the context in the tests?

From Dev

How to add operator extension as a part of context of the specific class without subclassing?

Related Related

  1. 1

    Android inner classes memory leak and leak by context?

  2. 2

    How to replace one dynamic array with another without causing memory leak?

  3. 3

    How to use Shared Preferences in MVP without Dagger and not causing Presenter to be Context dependent?

  4. 4

    How to access the .class object in java without using the class name from a static context

  5. 5

    How to avoid memory leak in context.getSystemService(Context.CAMERA_SERVICE)?

  6. 6

    Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)

  7. 7

    "Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"

  8. 8

    Using $this when not in object context without the use of static methods

  9. 9

    Passing @Context argument to method in class

  10. 10

    Passing @Context argument to method in class

  11. 11

    Will passing context to helper class in android activity leak?

  12. 12

    How to use a std::mutex in a class context

  13. 13

    Why/How is my code causing a memory leak?

  14. 14

    Getting Context from other class causing NPE

  15. 15

    cannot use this in a static context activity

  16. 16

    How to pass context or any parameter in a static class in flutter?

  17. 17

    How to pass Context into public class with public static String method

  18. 18

    How to get context of class

  19. 19

    Android context memory leak via activity private member

  20. 20

    Possible to free Context from Singleton in Android to prevent memory leak

  21. 21

    Possible to free Context from Singleton in Android to prevent memory leak

  22. 22

    why is std::string causing a memory leak in a class even after deleting

  23. 23

    How to access a file in a static context?

  24. 24

    How to Wait a Thread in a Static Context?

  25. 25

    how to use the context in the tests?

  26. 26

    How to use context in a function?

  27. 27

    How to use 'this' context in middleware

  28. 28

    how to use the context in the tests?

  29. 29

    How to add operator extension as a part of context of the specific class without subclassing?

HotTag

Archive