简体   繁体   中英

Navigation working in one webview but not another - Android Studio

I am creating an application for my robotics team so that we can quickly browse a few important websites. The issue I am having is that I can navigate through one of the fragments using the back button(until it exits from the app), but the other fragment titled "delphiFragment" is not able to navigate backwards through the website.

If there is any other information I can provide, please say so.

Here is my main activity

package com.thirdspare.android.ckcyberpackapp;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.TextView;

import static com.thirdspare.android.ckcyberpackapp.R.id.container;


public class MainActivity extends AppCompatActivity {
    Fragment homeFragment = new HomeFragment();
    Fragment delphiFragment = new DelphiFragment();
    Fragment tbaFragment = new TBAFragment();
    private TextView mTextMessage;
    public static WebView mWebView;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                   mTextMessage.setText(R.string.title_home);
                    getFragmentManager().beginTransaction()
                    .show(homeFragment)
                    .commit();
                    return true;
                case R.id.navigation_delphi:
                   // mTextMessage.setText(R.string.title_delphi);
                    getFragmentManager().beginTransaction()
                            .show(delphiFragment)
                            .hide(homeFragment)
                            .hide(tbaFragment)
                            .commit();

                    return true;
                case R.id.navigation_TBA:
                   mTextMessage.setText(R.string.title_tba);
                    getFragmentManager().beginTransaction()
                            .show(tbaFragment)
                            .hide(homeFragment)
                            .hide(delphiFragment)
                            .commit();
                    return true;
            }
            return false;
        }

    };
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }

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



        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        if (savedInstanceState == null) {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(container, homeFragment);
            ft.add(container, delphiFragment);
            ft.add(container, tbaFragment);
            ft.hide(delphiFragment);
            ft.hide(tbaFragment);
            ft.commit();
        }
    }




}

And here is my delphiFragment class

package com.thirdspare.android.ckcyberpackapp;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static com.thirdspare.android.ckcyberpackapp.MainActivity.mWebView;


public class DelphiFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parentViewGroup, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_delphi, parentViewGroup, false);
        mWebView = (WebView)view.findViewById(R.id.wbvCD);
        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.loadUrl("https://www.chiefdelphi.com/forums/portal.php");
        return view;



    }
}

Which is almost identical to the other webview fragment

In MainActivity your are adding three fragments and hide two other fragments. Later on your demand your hiding and showing fragment. Based on my understanding hide will disable fragment view from window, but it is still in running state of its lifecycle. So the static mWebview has only one of the fragment intstance all the time, mWebview didn't change based on your selection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM