[英]redirection from one fragment to another by clicking a button in one fragment together with changing the bottom navigation icon in android studio
[英]Navigation working in one webview but not another - Android Studio
我正在為我的機器人團隊創建一個應用程序,以便我們可以快速瀏覽一些重要的網站。 我遇到的問題是,我可以使用“后退”按鈕瀏覽一個片段(直到從應用程序退出),而另一個名為“ delphiFragment”的片段卻無法在網站上向后瀏覽。
如果還有其他我可以提供的信息,請這樣說。
這是我的主要活動
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();
}
}
}
這是我的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;
}
}
這幾乎與其他Webview片段相同
在MainActivity中,您將添加三個片段並隱藏另外兩個片段。 稍后您可以根據需要隱藏和顯示片段。 根據我的理解,hide將禁用窗口中的片段視圖,但它仍處於其生命周期的運行狀態。 因此,靜態mWebview始終始終只有一個片段實例,而mWebview不會根據您的選擇進行更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.