Josh
Josh

Reputation: 851

Call WebView in MainActivity from menu fragment onClick

I have an activity with an oncreate that i previously had just been calling a void function to load a url in a webview

private CharSequence mTitle;
private String mPageURL;

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    mPageURL = "http://beta.html5test.com/";
    loadWebViewContent();
}

public void loadWebViewContent() {
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.setWebViewClient(new WebViewClient());
    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    //mPageURL = "http://beta.html5test.com/";
    mWebView.loadUrl(mPageURL);
}

Now that I have that working however, I am trying to get a menu item click to call loadWebViewContent() in order to load a different page.

I have an onClick listener setup in my NavigationDrawerFragment

@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
            Toast.makeText(getActivity(), "test"+position, Toast.LENGTH_LONG).show();
        }

But I can't figure out how to call the function from here, or if necessary, what needs to be done to move the loadWebViewContent() to the fragment (since it is a menu thing, i'm ok with this), but from what I understand the WebView is owned by the Activity, so i'll still have to do something i'm not doing to access that within the function and tell it to load the url.

Any help or pointing in the right direction is much appreciated, thanks.

Upvotes: 0

Views: 736

Answers (1)

Subham
Subham

Reputation: 503

you can have one callback from fragment to activity for example

 public class NavigationDrawerFragment{
        private Callback mCallback;

        public void setCallback(Callback callback){
            mCallback = callback;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
            Toast.makeText(getActivity(), "test"+position, Toast.LENGTH_LONG).show();
            mCallback.openUrl("your url");
        }

        public interface CallBack{
            public void openUrl(String urlToOprn);
        }

    }

In you activity implements this interface like-

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

                mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
                mNavigationDrawerFragment.setCallback(this);
            ------you code from here

            }

            @Override
            public void openUrl( String urlToOpen){
                mPageURL = urlToOpen;
                loadWebViewContent();
            }   

        }

Upvotes: 1

Related Questions