[Solved] How to use “pauseTimers”


You’re trying to access a non-static method in a static way. Firstly a quick explaination of what a static method does. Consider this class:

public class ClassA {
    public void methodOne() {}
    public static void methodTwo() {}
}

methodOne is an Instance method. This can only be called from an instance of ClassA declared with the new keyword, like so:

ClassA myClassA = new ClassA();
myClassA.methodOne();

methodTwo is static, therefore it belongs to the class itself, and not any particular instance of that class. The downside of this is that static methods cannot access member variables unless they too are declared static:

ClassA.methodTwo();

In your case, you need to call pauseTimers() on the instance of the WebView object you’ve declared in your Activity or layout. So from your code posted above:

private WebView myWebView; // Move this declaration out from onCreate()

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    myWebView = (WebView) findViewById(R.id.webview);

    // rest of the method...
}

@Override
public void onPause() {
    super.onPause();
    myWebView.pauseTimers();
}

1

solved How to use “pauseTimers”