我正在为我的机器人团队创建一个应用程序,以便我们可以快速浏览一些重要的网站。我遇到的问题是我可以使用后退按钮浏览其中一个片段(直到它从应用程序退出),但另一个片段标题为" 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片段相同
答案 0 :(得分:0)
在MainActivity中,您正在添加三个片段并隐藏另外两个片段。稍后您要求隐藏和显示片段。基于我的理解,hide将禁用窗口中的片段视图,但它仍然处于其生命周期的运行状态。因此,静态mWebview始终只有一个片段intstance,mWebview没有根据你的选择而改变。