0

I am really confused with all this application/activity context stuff. I want to call some of activity methods from classes, such as my own WebChromeClient or ConnectionChangeReceiver (detecting network connection changes).

Example code:

public class MyWebChromeClient extends WebChromeClient {

    MyWebView webview;
    MyApp app;

// own constructor to store info about webview which is used in the current    
//webchrome client
    public MyWebChromeClient(Context context, MyWebView wv)
    {
        this.webview = wv; // store information about current webview (leaks?)
        this.app = context.getApplicationContext(); // store app context 
    }

    public void onConsoleMessage(String message, int lineNumber, String sourceID) {
           app.getActivityWhichIsUsingThisWebView().logFromWebView(webview, message); // ??
      }

}

How to achieve that? Is there any sensible pattern which could help me avoiding memory leaks?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Piotr
  • 1,743
  • 1
  • 26
  • 40

1 Answers1

1

i'm not quite sure.

Activity activity;

public MyWebChromeClient(Context context, MyWebView wv,Activity activity)
{
    this.webview = wv; // store information about current webview (leaks?)
    this.app = context.getApplicationContext(); // store app context 
    this.activity = activity;
}
April Smith
  • 1,810
  • 2
  • 28
  • 52
  • Isn't it causing memory leak? ( http://developer.android.com/resources/articles/avoiding-memory-leaks.html ) "Try using the context-application instead of a context-activity" – Piotr Dec 28 '11 at 10:35