Я пытаюсь изменить код в Google, но ничего из этого не работает для меня.поэтому у меня есть 2 фрагмента (с веб-просмотром), 1-й фрагмент переходит на youtube.com, а 2-й фрагмент - на google.com
, поэтому, когда мое приложение запускается, оно начинается с 1-го фрагмента, а затем, когда я перехожу ко 2-му фрагменту.и возвращается к 1-ому, он перезагружает его снова, но я хочу, чтобы он сохранял свое состояние и никогда не перезагружался при изменении фрагмента, но фрагмент (веб-просмотр) должен обновляться, когда пользователь закрывает приложение и запускается снова. Просто используйте навигационный ящик для переключения междуфрагмент
вот мой код для 1-го фрагмента
/**
* A simple {@link Fragment} subclass.
* created ny anshul
*/
public class BlankFragment12 extends Fragment {
public Handler h;
ProgressBar progressBar;
Bundle webViewBundle;
public AdView adView;
public WebView mWebView;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mWebView.restoreState(savedInstanceState);
}
public BlankFragment12() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view
= inflater.inflate(R.layout.blank_fragment12, container, false);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
progressBar.setMax(100);
progressBar.setProgress(1);
mWebView = (WebView) view.findViewById(R.id.hu);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setDisplayZoomControls(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setVerticalScrollBarEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.setHorizontalScrollBarEnabled(true);
this.mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mWebView.loadUrl("https://youtube.com/");
AdRequest adRequest = new AdRequest.Builder().build();
adView = (AdView) view.findViewById(R.id.ad9View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad10View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad13View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad14View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad11View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad12View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn1);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn2);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn3);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn4);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn5);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad103);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn13);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn23);
adView = (AdView) view.findViewById(R.id.earn33);
adView.loadAd(adRequest);
adView.loadAd(adRequest);
mWebView.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return false;
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("file:///android_asset/error.html");
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
});
return view;
}
}
код для 2-го фрагмента
/**
* A simple {@link Fragment} subclass.
* created by anshul
*/
public class BlankFragment2 extends Fragment {
public Bundle webViewBundle;
public Handler h;
ProgressBar progressBar;
public AdView adView;
public WebView mWebView;
SwipeRefreshLayout swipe;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mWebView.restoreState(savedInstanceState);
}
public BlankFragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ) {
setRetainInstance(true);
// Inflate the layout for this fragment
View view
= inflater.inflate(R.layout.fragment_blank_fragment3, container, false);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
progressBar.setMax(100);
progressBar.setProgress(1);
mWebView = (WebView) view.findViewById(R.id.hu);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setDisplayZoomControls(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.getSettings().setAllowFileAccess(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
if (Build.VERSION.SDK_INT >= 19) {
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.setHorizontalScrollBarEnabled(true);
this.mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadUrl("https://google.com");
AdRequest adRequest = new AdRequest.Builder().build();
adView = (AdView) view.findViewById(R.id.ad9View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad10View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad11View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad12View);
adView = (AdView) view.findViewById(R.id.ad13View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad14View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn1);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn2);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn3);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn4);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn5);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad103);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn13);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn23);
adView = (AdView) view.findViewById(R.id.earn33);
adView.loadAd(adRequest);
adView.loadAd(adRequest);
mWebView.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return false;
}
}); mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("file:///android_asset/error.html");
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
});
return view; }
}
onNavigationItemВыбранный код
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
BlankFragment blankFragment = new BlankFragment();
FragmentManager manager = getSupportFragmentManager();
manager .beginTransaction().addToBackStack(null).replace(R.id.oou, blankFragment).commit();
}else if (id == R.id.nav_gallery) {
BlankFragment4 blankFragment4 = new BlankFragment4();
FragmentManager manager = getSupportFragmentManager();
manager .beginTransaction().addToBackStack(null).replace(R.id.oou, blankFragment4).commit();
}
else if (id == R.id.i) {
BlankFragment7 blankFragment7 = new BlankFragment7();
FragmentManager manager = getSupportFragmentManager();
manager .beginTransaction().addToBackStack(null).replace(R.id.oou, blankFragment7).commit();
}
else if (id == R.id.sav) {
BlankFragment12 blankFragment12 = new BlankFragment12();
FragmentManager manager = getSupportFragmentManager();
manager .beginTransaction().addToBackStack(null).replace(R.id.oou, blankFragment12).commit();
}
else if (id == R.id.home) {
BlankFragment2 blankFragment2 = new BlankFragment2();
FragmentManager manager = getSupportFragmentManager();
manager .beginTransaction().addToBackStack(null).replace(R.id.oou, blankFragment2).commit();
}else if (id == R.id.nav_slideshow) {
BlankFragment3 blankFragment3 = new BlankFragment3();
FragmentManager manager = getSupportFragmentManager();
manager .beginTransaction().addToBackStack(null).replace(R.id.oou, blankFragment3).commit();
} else if (id == R.id.nav_manage) {
BlankFragment9 blankFragment9 = new BlankFragment9();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().addToBackStack(null).replace(R.id.oou,blankFragment9).commit();
}
else if (id == R.id.coll) {
BlankFragment11 blankFragment11 = new BlankFragment11();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().addToBackStack(null).replace(R.id.oou,blankFragment11).commit();
}
else if (id == R.id.bf13) {
BlankFragment13 blankFragment13 = new BlankFragment13();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().addToBackStack(null).replace(R.id.oou,blankFragment13).commit();
}
else if (id == R.id.bf14) {
BlankFragment14 blankFragment14 = new BlankFragment14();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().addToBackStack(null).replace(R.id.oou,blankFragment14).commit();
}
else if (id == R.id.bf15) {
BlankFragment15 blankFragment15 = new BlankFragment15();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().addToBackStack(null).replace(R.id.oou,blankFragment15).commit();
}
else if (id == R.id.fav) {
BlankFragment10 blankFragment10 = new BlankFragment10();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().addToBackStack(null).replace(R.id.oou,blankFragment10).commit();
} else if (id == R.id.nav_send) {
BlankFragment8 blankFragment8 = new BlankFragment8();
FragmentManager manager = getSupportFragmentManager();
manager .beginTransaction().addToBackStack(null).replace(R.id.oou, blankFragment8).commit();
}
else if (id == R.id.da) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=hoogle")));
}
else if (id == R.id.d) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.hoogle.news2")));
}
else if (id == R.id.funny) {
BlankFragment6 blankFragment6 = new BlankFragment6();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().addToBackStack(null).replace(R.id.oou,blankFragment6).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
приветствуются дополнительные предложения по более плавному / более качественному просмотру веб-страниц