I want to implement the back button to my app. I'm using fragments that each show a different webview. Right now if I press the back button, it closes the app no matter where I am. I would like for it to go back, just like a browser. I have my MainActivity.java
, fragment1.java
, etc up to fragment5.java
Here is my MainActivity.java
:
package com.----;
import java.util.Locale;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case 0:
fragment = new fragment1();
break;
case 1:
fragment = new fragment2();
break;
case 2:
fragment = new fragment3();
break;
case 3:
fragment = new fragment4();
break;
case 4:
fragment = new fragment5();
break;
default:
fragment = null;
break;
}
return fragment;
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.tab1).toUpperCase(l);
case 1:
return getString(R.string.tab2).toUpperCase(l);
case 2:
return getString(R.string.tab3).toUpperCase(l);
case 3:
return getString(R.string.tab4).toUpperCase(l);
case 4:
return getString(R.string.tab5).toUpperCase(l);
}
return null;
}
}
}
And here is my fragment1.java:
package com.----;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
public class fragment1 extends Fragment {
WebView myWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment1, container, false);
myWebView = (WebView) root.findViewById(R.id.webview1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("url");
return root;
}
}
If I try to use this solutions and I get a force close when I'm testing it and press the back button.
Here is a logcat of it.
In your activity override on backpressed:
@Override
public void onBackPressed() {
switch (mViewPager.getCurrentItem()) {
case 0:
if (!webViewGoBack(0)) {
//do something if webview cannot go back
}
break;
case 1:
break;
default:
}
}
public boolean webViewGoBack(int num) {
SectionsPagerAdapter adapter = ((SectionsPagerAdapter)mViewPager.getAdapter());
Fragment f = (Fragment )adapter.getFragment(num);
if (f!= null) {
return f.webViewGoBack();
}
return false;
}
f.webViewGoBack() the method in you fragment:
public boolean WebViewGoBack() {
if(webView.canGoBack()){
webView.goBack();
return true;
}
return false; //webview cannot go back, so use the method of the BackButton
}
Just override onBackPressed() methid of your Activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
First first_act = new First();
fragmentTransaction.replace(R.id.fragment_container,
first_act);
fragmentTransaction.addToBackStack("first_act");
fragmentTransaction.commit();
Add these code in mainactivity , and then use this onbackpressed in fragment
@Override
public void onBackPressed() {
}
You can begin by overriding the back button press in your MainActivity
@Override
public void onBackPressed() {
//super.onBackPressed()
//handle the press here to switch fragments
}
来源:https://stackoverflow.com/questions/18842137/implementing-back-button-in-webview-fragment