-1

Simple question, how do i call a function/method from inside shouldOverrideUrlLoading? I am searching for something like Qt's Signal and Slots so that i can get around that OO-Can-Not-See-Your-Parent-Jail. Using a static is sadly not an option as then i can not access the UI from that static anymore. And setting up the UI elements i want to access as statics wont work either as the Android API changes these around all the time, invalidating the static representations.

Thats what i have right now:

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView wv = (WebView) this.findViewById(R.id.webView);

        wv.setWebViewClient(new WebViewClient()  {
            @Override
            public boolean shouldOverrideUrlLoading(WebView wv, String url) 
            {
                //TODO: Call doUrlHandling somehow...
                //wv.loadData("URL:" + url, "text/html", "UTF-8");
                return true;
            }
        });

    wv.loadUrl("file:///android_asset/startup.html");
    }

    public void doUrlHandling(String url)
    { }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }   
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
BastetFurry
  • 299
  • 3
  • 18

1 Answers1

1

You can either save a handle to your activity/context:

  public class MainActivity extends Activity
    {

     private MainActivity mActivity;

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

        mActivity = this;                          // Save handle here

        setContentView(R.layout.activity_main);
    .
    .
    .

        wv.setWebViewClient(new WebViewClient()  {
            @Override
            public boolean shouldOverrideUrlLoading(WebView wv, String url) 
            {

                mActivity.doUrlLoading(url);        // call method of MainActivity

                return true;
            }
        });

    .
    .
    .

or reference the function from your inner class via:

MainActivity.this.doUrlLoading(url);

see Meaning of .this and .class in java for a great explanation of this syntax

Community
  • 1
  • 1
CSmith
  • 13,318
  • 3
  • 39
  • 42
  • Thanks :) Thats explained nowhere, every example i looked up just fills up the log and never tries to leave that function. – BastetFurry Dec 23 '13 at 20:02