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

John Smith

Android Studio:

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

So 2 questions:

#1 How do you call a startService from a static method without a static variable for context?
#2 How do you send a localBroadcast from a static method (same)?

Examples:

public static void log(int iLogLevel, String sRequest, String sData) {
    if(iLogLevel > 0) {

        Intent intent = new Intent(mContext, LogService.class);
        intent.putExtra("UPDATE_MAIN_ACTIVITY_VIEW", "UPDATE_MAIN_ACTIVITY_VIEW");
        mContext.startService(intent);
    }
}

or

        Intent intent = new Intent(MAIN_ACTIVITY_RECEIVER_INTENT);
        intent.putExtra(MAIN_ACTIVITY_REQUEST_FOR_UPDATE, sRequest));
        intent.putExtra(MAIN_ACTIVITY_DATA_FOR_VIEW, sData);
        intent.putExtra(MAIN_ACTIVITY_LOG_LEVEL, iLogLevel);
        LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);

What would be the correct way to do this without using mContext?

NOTE: I think my main question might be how to pass context to a class from which the calling method lives.

AndroidMechanic - Viral Patel

Simply pass it as a parameter to your method. There is no sense in creating a static instance of Context solely for the purpose of starting an Intent.

This is how your method should look:

public static void log(int iLogLevel, String sRequest, String sData, Context ctx) {
    if(iLogLevel > 0) {

        Intent intent = new Intent(ctx, LogService.class);
        intent1.putExtra("UPDATE_MAIN_ACTIVITY_VIEW", "UPDATE_MAIN_ACTIVITY_VIEW");
        ctx.startService(intent);
    }
}

Update from comments on question: Cascade the context from the initiating activity (via constructor parameters or method parameters) right up to the point you need it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do final fields NOT leak memory?

From Dev

Android - Device Memory Leak with Fragments

From Dev

Android Fragment Webview Memory Leak

From Dev

Why does Android leak memory due to static Drawable if it's callback is reset?

From Dev

Android context memory leak via activity private member

From Dev

Android runOnUiThread causing memory leak

From Dev

Android memory leak on static Resource member variable?

From Dev

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

From Dev

Jersey Client, memory leak, static and concurrency

From Dev

Android animations memory leak

From Dev

Is it a memory leak in android

From Dev

Android camera Bitmap memory leak

From Dev

Android inner classes memory leak and leak by context?

From Dev

SpriteKit Memory Leak on static Menu scene

From Dev

Android Studio 2.0 Instant Run causes DexFile to not load all classes

From Dev

Android progress bar memory leak

From Dev

Tomcat memory leak warning on HttpURLConnection

From Dev

`Unknown` (`Other`) memory leak in Android?

From Dev

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

From Dev

Android runOnUiThread causing memory leak

From Dev

memory leak with Android WebView

From Dev

How do final fields NOT leak memory?

From Dev

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

From Dev

Jersey Client, memory leak, static and concurrency

From Dev

Is it a memory leak in android

From Dev

SpriteKit Memory Leak on static Menu scene

From Dev

Prevent memory leak in Android

From Dev

Android - is this a memory leak?

From Dev

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

Related Related

  1. 1

    How do final fields NOT leak memory?

  2. 2

    Android - Device Memory Leak with Fragments

  3. 3

    Android Fragment Webview Memory Leak

  4. 4

    Why does Android leak memory due to static Drawable if it's callback is reset?

  5. 5

    Android context memory leak via activity private member

  6. 6

    Android runOnUiThread causing memory leak

  7. 7

    Android memory leak on static Resource member variable?

  8. 8

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

  9. 9

    Jersey Client, memory leak, static and concurrency

  10. 10

    Android animations memory leak

  11. 11

    Is it a memory leak in android

  12. 12

    Android camera Bitmap memory leak

  13. 13

    Android inner classes memory leak and leak by context?

  14. 14

    SpriteKit Memory Leak on static Menu scene

  15. 15

    Android Studio 2.0 Instant Run causes DexFile to not load all classes

  16. 16

    Android progress bar memory leak

  17. 17

    Tomcat memory leak warning on HttpURLConnection

  18. 18

    `Unknown` (`Other`) memory leak in Android?

  19. 19

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

  20. 20

    Android runOnUiThread causing memory leak

  21. 21

    memory leak with Android WebView

  22. 22

    How do final fields NOT leak memory?

  23. 23

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

  24. 24

    Jersey Client, memory leak, static and concurrency

  25. 25

    Is it a memory leak in android

  26. 26

    SpriteKit Memory Leak on static Menu scene

  27. 27

    Prevent memory leak in Android

  28. 28

    Android - is this a memory leak?

  29. 29

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

HotTag

Archive