Will passing context to helper class in android activity leak?

akdeniza

If I have a Helper Class like the following:

public class TestHelper {
  private Context context;

  public TestHelper(Context context);
    this.context = context;
  }

  public doSomethingWithContext(){
    //some code
   }

and my Activity looks like the following:

 public class MainActivity extends AppCompatActivity{
  private TestHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       helper = new TestHelper(this);
    }

 }

Will the context be leaked like this or do I have to set helper to null in the onDestroy method (would this even work for the GC)?

Also I need the helper in several methods (onCreate, onPause etc) so creating the Helper inside in all of the methods doesn't sound like a good solution.

pitfall

Context doesn't leak in your code because GC correctly handles cyclic references. See explanation here

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: Passing context to helper class results in NPE

From Dev

passing context to non-activity class for a dbHelper in Android

From Dev

Trouble passing context to helper class method

From Dev

Android: avoiding passing activity to singletons by storing inside the Application class = memory leak?

From Dev

Android context memory leak via activity private member

From Dev

Android - Passing View from Activity to AsyncTask class

From Dev

Passing reference to activity to utility class android

From Dev

Android: "Expression expected" when passing an Activity class

From Dev

Xamarin Android passing data from class to activity

From Dev

android get activity context in abstract base class

From Dev

Android get context from non Activity Class

From Dev

Android: Get activity context into array adapter class

From Dev

Android: transferring Context and activity from a fragment to a class

From Dev

Android passing arguments to activity from non-activity class

From Dev

How to get activity context into a non-activity class android?

From Dev

Android findViewById in activity method is not working if is called from helper class

From Dev

Android inner classes memory leak and leak by context?

From Dev

Android: Passing value from view class to another activity back and forth

From Dev

Passing a portable Class to an Activity with Xamarin's Mono for Android

From Dev

Passing value to class that doesn't extends Activity (Android)

From Dev

How can I get the context of a class (non-activity) in Android?

From Dev

Best way to get Context in android for long running non Activity Class?

From Dev

Android Issue with activity context

From Dev

Android Activity Context is Null

From Dev

Class to Activity passing ArrayList values

From Dev

Passing Application Context in Activity in Dagger 2 (NullPointerException)

From Dev

Application Context in Non Activity Class?

From Dev

Get the context of Activity in a Serializable class

From Dev

Passing entity context into class constructor

Related Related

  1. 1

    Android: Passing context to helper class results in NPE

  2. 2

    passing context to non-activity class for a dbHelper in Android

  3. 3

    Trouble passing context to helper class method

  4. 4

    Android: avoiding passing activity to singletons by storing inside the Application class = memory leak?

  5. 5

    Android context memory leak via activity private member

  6. 6

    Android - Passing View from Activity to AsyncTask class

  7. 7

    Passing reference to activity to utility class android

  8. 8

    Android: "Expression expected" when passing an Activity class

  9. 9

    Xamarin Android passing data from class to activity

  10. 10

    android get activity context in abstract base class

  11. 11

    Android get context from non Activity Class

  12. 12

    Android: Get activity context into array adapter class

  13. 13

    Android: transferring Context and activity from a fragment to a class

  14. 14

    Android passing arguments to activity from non-activity class

  15. 15

    How to get activity context into a non-activity class android?

  16. 16

    Android findViewById in activity method is not working if is called from helper class

  17. 17

    Android inner classes memory leak and leak by context?

  18. 18

    Android: Passing value from view class to another activity back and forth

  19. 19

    Passing a portable Class to an Activity with Xamarin's Mono for Android

  20. 20

    Passing value to class that doesn't extends Activity (Android)

  21. 21

    How can I get the context of a class (non-activity) in Android?

  22. 22

    Best way to get Context in android for long running non Activity Class?

  23. 23

    Android Issue with activity context

  24. 24

    Android Activity Context is Null

  25. 25

    Class to Activity passing ArrayList values

  26. 26

    Passing Application Context in Activity in Dagger 2 (NullPointerException)

  27. 27

    Application Context in Non Activity Class?

  28. 28

    Get the context of Activity in a Serializable class

  29. 29

    Passing entity context into class constructor

HotTag

Archive