我有一个MainActivity
和一些片段,我正在使用WebView
在所有片段中加载一些网站。在我按下后退按钮之前无法进入上一页但现在我能够。我有一切正常,但我无法在启动时加载片段。如果我在MainActivity
中添加此代码以自动加载片段,它可以正常工作,但它会破坏其他片段中的后退键导航,
getFragmentManager().beginTransaction()
.replace(R.id.content_frame
, new aboutus())
.addToBackStack(null).commit();
启动时默认情况下我是否可以通过其他方式加载片段?这是我的MainActivity
(包括自动打开片段的代码),
package com.science.s11;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.content_frame
, new aboutus())
.addToBackStack(null).commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (aboutus.mWebView!= null) {
if (aboutus.mWebView.canGoBack()) {
aboutus.mWebView.goBack();
} else {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
}else{
finish();}
}
}
else if(amp.mWebView!= null){
if ( amp.mWebView.canGoBack()){
amp.mWebView.goBack();
}else {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
}else{
finish();}
}
}
else if(ask.mWebView!= null){
if ( ask.mWebView.canGoBack()){
ask.mWebView.goBack();
}else {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
}else{
finish();}
}
}
else if(feedback.mWebView!= null){
if ( feedback.mWebView.canGoBack()){
feedback.mWebView.goBack();
}else {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
}else{
finish();}
}
}
else if(subscribe.mWebView!= null){
if ( subscribe.mWebView.canGoBack()){
subscribe.mWebView.goBack();
}else {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
}else{
finish();}
}
}
else{
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
android.app.FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_aboutus) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new aboutus())
.addToBackStack(null).commit();
// Handle the camera action
} else if (id == R.id.nav_amp) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new amp())
.addToBackStack(null).commit();
} else if (id == R.id.nav_ask) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new ask())
.addToBackStack(null).commit();
} else if (id == R.id.nav_feedback) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new feedback())
.addToBackStack(null).commit();
} else if (id == R.id.nav_subscribe) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new subscribe())
.addToBackStack(null).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
以下是我所有片段中的示例代码
package com.science.s11;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.KeyEvent;
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 android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.net.NetworkInfo;
public class aboutus extends Fragment {
public static WebView mWebView;
public ProgressBar progressBar;
public LinearLayout layoutProgress;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.aboutus, container, false);
mWebView = (WebView) v.findViewById(R.id.aboutuswebView);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
layoutProgress = (LinearLayout) v.findViewById(R.id.layoutProgress);
mWebView.setVisibility(View.GONE);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
WebSettings settings = mWebView.getSettings();
settings.setSupportMultipleWindows(true);
settings.setBuiltInZoomControls(false);
settings.setSupportZoom(false);
settings.setDisplayZoomControls(false);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
mWebView.setVisibility(View.VISIBLE);
layoutProgress.setVisibility(View.GONE);
progressBar.setIndeterminate(true);
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
layoutProgress.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(false);
super.onPageStarted(view, url, favicon);
}
});
if (isOnline()) {
mWebView.loadUrl("http://science19.com/index.php");
} else {
mWebView.loadUrl("file:///android_asset/cti.html");
}
return v;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return onKeyDown(keyCode, event);
}
public static boolean canGoBack(){
return mWebView.canGoBack();
}
public static void goBack(){
mWebView.goBack();
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return (netInfo != null && netInfo.isConnected());
}
}
编辑(补充说明):我实际上想在app启动时显示片段,片段包含WebView。我可以使用此代码来做到这一点
getFragmentManager().beginTransaction()
.replace(R.id.content_frame
, new aboutus())
.addToBackStack(null).commit();
但我甚至还有其他片段,每个人都有能力回到上一页。每当我使用这段代码时,我都无法在其他片段的背面按回到上一页,它只能在使用代码打开的片段中工作,这里是 aboutus 。那么,为什么这实际发生了,我该如何解决呢?
答案 0 :(得分:1)
使用 AppCompatActivity
getSupportFragmentManager()
而不是
getFragmentManager()