• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Acessing my WebView object from another class

sega dude

Member
I am creating an Android version of my web browser. I want to be able to load websites from another class using the WebView object that is in my MainActivity class. Basically I want to load a website from a button referenced in the QuickSites class in the WebView object in the MainActivity class. How would I do this? Please give me example code if possible as I am new to Android development. Thanks!
 
I'm not a JAVA pro either. (I'm more towards C++)

I'd say it depends on how you create your QuickSites instance...
it'd be nice if you can show us your code.

most simple way,
Code:
class MainActivity {
    [B]public static[/B] WebView wv = null;
}
perhaps you can give the webview a static attribute?
then you can access it statically?

Code:
class QuickSites {
    button.onClick() {
        [B]MainActivity.wv[/B].doSomething();
    }
}
else, you will probably need a reference of MainActivity inside QuickSites.
 
just reminded,
or maybe you can do an Intent.
the Intent that's used to start up another Activity.

Code:
Intent i = new Intent(this, MainActivity.class);
i.putExtra([COLOR=blue]"act"[/COLOR], "openPage");
i.putExtra([COLOR=blue]"url"[/COLOR], [COLOR=blue]"http://web.com"[/COLOR]);
grab and use the data, then skip all the view creating and stuff.
 
I'm not a JAVA pro either. (I'm more towards C++)

I'd say it depends on how you create your QuickSites instance...
it'd be nice if you can show us your code.

most simple way,
Code:
class MainActivity {
    [B]public static[/B] WebView wv = null;
}
perhaps you can give the webview a static attribute?
then you can access it statically?

Code:
class QuickSites {
    button.onClick() {
        [B]MainActivity.wv[/B].doSomething();
    }
}
else, you will probably need a reference of MainActivity inside QuickSites.

Thank you! That worked great! It's a simple, yet effective way to go it. Thank you so much! Your help is greatly appreciated.
 
Back
Top Bottom