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

Apps Start MainActivity with different WebView loadUrl

wavix

Lurker
Hello!

In MainActivity I have this mWebView which changes loadUrl depending on the user action. I want this to happen from different class too.

Code:
setContentView(R.layout.activity_main);
 
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
 
    WebSettings webSettings = mWebView.getSettings();
 
    Context context = this;
    mWebView.getSettings().setGeolocationDatabasePath(context.getFilesDir().getPath());
 
    webSettings.setJavaScriptEnabled(true);
 
    mWebView.setWebViewClient(new WebViewClient());
 
    mWebView.setWebChromeClient(new WebChromeClient() {
       public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
       callback.invoke(origin, true, false);
       }
    });
 
    mWebView.loadUrl("http://google.com");

and here is how I change loadUrl in MainActivity:

Code:
@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
 
       int id = item.getItemId();
 
       if (id == R.id.firstmenu) {
          mWebView.loadUrl("http://yahoo.com");
       }
       else if (id == R.id.secondmenu) {
          mWebView.loadUrl("http://microsoft.com");
       }
 
    }

Now, in another class, ItemView, I want to switch to MainActivity but with different mWebView.loadUrl, depending on user action also. Hope you understand.

Here is what I tried in ItemView class:

Code:
@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
 
     int id = item.getItemId();
 
     if (id == R.id.firstmenu) {
      Intent i = new Intent(ItemView.this, MainActivity.class);
      startActivity(i);
 
      setContentView(R.layout.activity_main);
 
      mWebView = (WebView) findViewById(R.id.activity_main_webview);
      mWebView.loadUrl("http://yahoo.com");
     }
     else if (id == R.id.secondmenu) {
      Intent i = new Intent(ItemView.this, MainActivity.class);
      startActivity(i);
 
      setContentView(R.layout.activity_main);
 
      mWebView = (WebView) findViewById(R.id.activity_main_webview);
      mWebView.loadUrl("http://microsoft.com");
     }
 
    }

This switch my app to MainActivity but everytime loadUrl is url1. url2 or url3 opens in system browser.

How can I fix this? I even tried to copy mWebView settings from the MainActivity, without success.

Thanks!
 
It's not clear what you're trying to do. Possibly you need to pass the URL in the Intent, to the MainActivity.
 
I try to swith app to MainActivity with specific mWebView.loadUrl from another class.

Hope you understand now. Sorry if I was unclear.
 
Ok so if you want to transmit some data from one activity to another, the way to do this is to pass the data in the Intent.

http://developer.android.com/guide/components/intents-filters.html

Look at the sections on 'Data' and 'Extras'. There is loads of additional information/tutorials explaining how to do this.

An alternative way of allowing one class to access data in another is to hold it in a public static class member variable. Although I tend not to do this, as it breaks encapsulation.
 
Back
Top Bottom