Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeze or make Stable Web-view When I Select Action Bar Back?Prevent Loading/reloading every time

I want to Prevent Page reload or Freeze the Current Web-view When I Select any menu from option menu

This is My Webview

    public class MyWebV extends AppCompatActivity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.mwview);


        Toolbar toolbar = (Toolbar) findViewById(R.id.tb1);
        setSupportActionBar(toolbar);

        webView = (WebView) findViewById(R.id.web5);
        WebSettings set = webView.getSettings();
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());
        set.setJavaScriptEnabled(true);
        set.setJavaScriptCanOpenWindowsAutomatically(true);
        set.setLoadWithOverviewMode(true);
        set.setUseWideViewPort(true);
        set.setDomStorageEnabled(true);
        set.setAllowUniversalAccessFromFileURLs(true);
        set.setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        final ProgressDialog progressBar = ProgressDialog.show(MyWebV.this, "Please Wait", "Loading...");
        webView.requestFocusFromTouch();
        webView.setWebViewClient(new WebViewClient()
        {

           public void onLoadResource (WebView view, String url) {
                if (progressBar == null) {

                    progressBar.setTitle("Please Wait !");
                    progressBar.setMessage("Loading...");
                    progressBar.show();
                }
            }

            public void onPageFinished(WebView view, String url) {

                try{
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }catch(Exception exception){
                    exception.printStackTrace();
                }
            }

        });

        webView.setWebChromeClient(new WebChromeClient() {


        String url = "www.example.com/login.php";
        webView.loadUrl(url);

    }

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);

        webView.saveState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        webView.restoreState(savedInstanceState);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.m_MyWebV, menu);


        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.about1) {
            Intent aboutIntent = new Intent(this, About.class);
            startActivity(aboutIntent);
            return true;
        }
        else if (id == R.id.set1) {
            Intent webIntent = new Intent(this, Settings.class);
            startActivity(webIntent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

}

I My Web-view I have Some Menus So When I click on any menu like Setting or About... It is showing But After that When I go back to Home with Option or Back arrow from Action Bar or Option Menu Its Reloading

So I need to login and And check the previous page its difficult for every time.. Can any one suggest me how to freeze web-view or How to prevent reload with the same app menus...

Update

This is my about Us class

public class About extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aboutus);

    Toolbar toolbar = (Toolbar) findViewById(R.id.tb3);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.m_about, menu);

     /*something*/
     return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.home) {
        Intent mypIntent = new Intent(this, MyWebV.class);
        About.this.finish();
        startActivity(mypIntent);
        return true;
    }
    else if (id == R.id.about1) {
        Intent aboutIntent = new Intent(this, About.class);
        About.this.finish();
        startActivity(aboutIntent);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

so for example if this is action bar back enter image description here

so When I click on arrow Its reloading the webview.. On device Hardware back I have disabled... on hardware back its fine but... On action bar back its Reloading

like image 308
Whats Going On Avatar asked Apr 07 '17 07:04

Whats Going On


People also ask

What is Android system WebView and do I need it?

Why is Android System WebView needed? Android System WebView lets applications display browser windows in an app instead of transporting the user to another browser. Android developers use WebView when they want to display webpages in a Google app or other application.

What is a WebView and how do you Add one to your layout?

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application. This method specifies the WebView has a back history item.


1 Answers

I have just performed a test using two activities, a MainActivity containing a webView just like yours, and a menu button that opens a second activity (About.java), using the exact same intent that you used in onOptionsItemSelected().

After opening About activity from the menu, if I press my phone's hardware back button, it returns to MainActivity without reloading the contents of the WebView, so it works as you expect.

I think the problem you are experiencing is with the code you use to go back to the main activity. You may be using an Intent to open your main activity again from your secondary activities, causing a new instance of your main activity to be created, triggering onCreate(), and thus re-creating your WebView and reloading the page.

To test if that's the case, you can add some debugging statements inside onCreate() using Log.d() or System.out.println(), to check if it's being called when you try to return to your main activity (MyWebV). If you see your debug messages in the logs, it's because you are not returning to your Main Activity properly.

The way you should close your secondary activities to return to your main activity is calling:

finish()

That method will close current activity and restore the previous activity in the stack (your MyWebV activity in this case), without re-creating it, just calling onStart() and onResume(), which wouldn't re-create your WebView or reload the page.

like image 121
Alejandro Núñez Avatar answered Oct 17 '22 16:10

Alejandro Núñez