简体   繁体   English

导航只能在一个Web视图中运行,而不能在另一个Web视图中运行-Android Studio

[英]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. 我遇到的问题是,我可以使用“后退”按钮浏览一个片段(直到从应用程序退出),而另一个名为“ delphiFragment”的片段却无法在网站上向后浏览。

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 这是我的delphiFragment类

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 这几乎与其他Webview片段相同

In MainActivity your are adding three fragments and hide two other fragments. 在MainActivity中,您将添加三个片段并隐藏另外两个片段。 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. 根据我的理解,hide将禁用窗口中的片段视图,但它仍处于其生命周期的运行状态。 So the static mWebview has only one of the fragment intstance all the time, mWebview didn't change based on your selection. 因此,静态mWebview始终始终只有一个片段实例,而mWebview不会根据您的选择进行更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过单击一个片段中的按钮并更改android studio中的底部导航图标,从一个片段重定向到另一个片段 - redirection from one fragment to another by clicking a button in one fragment together with changing the bottom navigation icon in android studio 当我在 Android Studio 中将一个页面导航到另一个页面时,如何从 WebView 中删除导航? - How to remove Nav from WebView, when I Navigate one page to another page in Android Studio? Webview 来自 Android Studio 确实显示导航菜单和背景徽标 - Webview from Android Studio does show a navigation menu and background logo Android Studio 中的底部导航栏不起作用? - Bottom Navigation Bar in Android Studio not Working? 导航菜单不适用于其他活动 - Android Studio - Navigation Menu Not Working For Other Activities - Android Studio 允许在 Android webview 中导航 - Allow navigation in Android webview 如何在Java中使用platform.runlater()禁用一个Web视图中的链接/超链接/导航并在另一个Web视图中打开该链接/超链接/导航 - How to use platform.runlater() to disable link/hyperlink/navigation in one webview and open that link/hyperlink/navigation in another webview in java Webview Java Android 工作室没有打开一页 - Webview Java Android Studio didnt open one page Android Studio:地理位置不起作用。 无法在Webview中获得地理位置 - Android Studio: geolocation not working. Failing to get geolocation in Webview WebView Android 工作室 - WebView Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM