4

hi i am facing android Webview orientation issue, when change orientation then application restart and show the page from start. I am reading different answere but no solution work me that why i am giving the my code here. kindly any android developer help me and tell me where i edit my code or add something

package com.example.edarabia;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;


@SuppressLint("SetJavaScriptEnabled")

public class MainActivity extends Activity{
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(isNetworkConnected() == true){
    setContentView(R.layout.activity_main);
    mywebview = (WebView) findViewById(R.id.webview);       
    mywebview.getSettings().setJavaScriptEnabled(true);
    mywebview.setWebViewClient(new myWebClient());
    mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");        
    mywebview.getSettings().setBuiltInZoomControls(true);
    mywebview.getSettings().setLoadWithOverviewMode(false);
    mywebview.getSettings().setUseWideViewPort(false);
    }else{
    setContentView(R.layout.splash);    
    showBuyDialog();
    }
}






//  @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
//        if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
//          mywebview.goBack();
//            return true;
 //       }

 //       return super.onKeyDown(keyCode, event);

//  }

 // To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
        mywebview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public void onBackPressed() {
    finish();
}


public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url);
        return true;
    }
}

//// This method will retun boolean value if net conect then value will be true otherwise false
private boolean isNetworkConnected() {
    ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
    }
    return false;
}

public void showBuyDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Unable to connect");

    builder.setMessage("You Must have an Internet connection to use Edarabia. Please connect and try again.");
    builder.setCancelable(false);
    builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            finish(); 
        }
    });
    builder.show();
}




}
  • http://stackoverflow.com/questions/1002085/android-webview-handling-orientation-changes – Walid Hossain Jan 18 '13 at 08:51
  • You could have a look on the following links: [activity-restart-on-rotation-android][1] [how-save-webview-when-screen-orientation-was-changed][2] [1]: http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android [2]: http://stackoverflow.com/questions/11121094/how-save-webview-when-screen-orientation-was-changed – Suji Jan 18 '13 at 08:56
  • already checked this solution, i am beginner in adnroid that why may be i implementing wrong that why i shared my code if walid you can help me with my code then i am thankful – user1474083 Jan 18 '13 at 08:57
  • already check all provided links but my application give error and say unfortunatelty has been stopped. please give me help – user1474083 Jan 18 '13 at 09:14

1 Answers1

5

Add android:configChanges="keyboard|keyboardHidden|screenSize|orientation" to your activity on the AndroidManifest.xml file. It should look like this

<activity
        android:name=".MainActivity"
        android:configChanges="keyboard|keyboardHidden|screenSize|orientation"
        android:label="@string/app_name" >

as seen here - Android WebView handling orientation changes

Community
  • 1
  • 1
Bruno
  • 311
  • 4
  • 4