我有一個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());
}
}
編輯(添加說明)示例代碼:其實我是想在應用程序啓動時顯示一個片段,片段包含WebView。我能做到這一點使用此代碼,
getFragmentManager().beginTransaction()
.replace(R.id.content_frame
, new aboutus())
.addToBackStack(null).commit();
但我甚至有其它片段和每一個人都有返回到前一頁背壓的能力。每當我使用這段代碼時,我不能回到前一頁上的其他片段上,它只是在使用代碼打開的片段中工作,在這裏它是關於我們。那麼,爲什麼這真的發生了,我該如何解決?
如果用'addToBackStack'添加'Fragment'您遇到任何錯誤 – vinoth12594
,它會被存儲在'BackStack'並在從堆棧彈出時進行訪問(例如,當您按下時)。刪除'addToBackStack'並且它會保持打開 - 如果它是你想要的。如果不是,請更具體地說明您的問題以及您想實現的目標。 :) – yennsarah
@ vinoth12594它不會給出任何錯誤 – aj20010319