android Webview方向问题,当更改方向后,应用程序重新启动并从一开始显示页面
您好,我正面临android Webview定向问题,当更改定向后,应用程序将重新启动并从一开始显示页面。我正在阅读不同的答案,但没有解决方案,这就是为什么我在这里给出我的代码。请所有android开发人员帮助我,告诉我在哪里编辑代码或添加内容android Webview方向问题,当更改方向后,应用程序重新启动并从一开始显示页面,android,webview,orientation,android-webview,Android,Webview,Orientation,Android Webview,您好,我正面临android Webview定向问题,当更改定向后,应用程序将重新启动并从一开始显示页面。我正在阅读不同的答案,但没有解决方案,这就是为什么我在这里给出我的代码。请所有android开发人员帮助我,告诉我在哪里编辑代码或添加内容 package com.example.edarabia; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDia
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();
}
}
package com.example.edarabia;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.graphics.Bitmap;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.os.Bundle;
导入android.view.KeyEvent;
导入android.webkit.WebView;
导入android.webkit.WebViewClient;
@SuppressLint(“SetJavaScriptEnabled”)
公共类MainActivity扩展了活动{
网络视图我的网络视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(isNetworkConnected()==true){
setContentView(R.layout.activity_main);
mywebview=(WebView)findViewById(R.id.WebView);
mywebview.getSettings().setJavaScriptEnabled(true);
setWebViewClient(新的myWebClient());
mywebview.loadUrl(“http://www.grafdom.com/operations/projects/ma/edarabiaapp/");
mywebview.getSettings().SetBuilTinZoomControl(true);
mywebview.getSettings().SetLoadWithOverview模式(false);
mywebview.getSettings().setUseWideViewPort(false);
}否则{
setContentView(R.layout.splash);
showBuyDialog();
}
}
//@覆盖
//公共布尔onKeyDown(int-keyCode,KeyEvent事件){
//检查键事件是否为后退按钮,是否有历史记录
//if((keyCode==KeyEvent.keyCode_BACK)&&mywebview.canGoBack()){
//mywebview.goBack();
//返回true;
// }
//返回super.onKeyDown(keyCode,event);
// }
//要处理WebView返回上一屏幕的“返回”按键事件。
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件)
{
if((keyCode==KeyEvent.keyCode_BACK)&&mywebview.canGoBack()){
mywebview.goBack();
返回true;
}
返回super.onKeyDown(keyCode,event);
}
@凌驾
public void onBackPressed(){
完成();
}
公共类myWebClient扩展了WebViewClient
{
@凌驾
public void onPageStarted(WebView视图、字符串url、位图favicon){
super.onPageStarted(视图、url、favicon);
}
@凌驾
公共布尔值应重写url加载(WebView视图,字符串url){
view.loadUrl(url);
返回true;
}
}
////此方法将返回布尔值,如果为净连接,则该值将为true,否则为false
专用布尔值isNetworkConnected(){
ConnectivityManager connectivity=(ConnectivityManager)getSystemService(Context.connectivity_服务);
if(连接性!=null){
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null)
对于(int i=0;i
将android:configChanges=“keyboard | keyboardHidden | Screen Size | orientation”添加到AndroidManifest.xml文件中的活动中。应该是这样的
<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|screenSize|orientation"
android:label="@string/app_name" >
您可以查看以下链接:[activity restart on rotation android][1][2][1]:[2]:已选中此解决方案,我是adnroid的初学者,为什么我可能实现错误,为什么我共享我的代码,如果你能帮助我使用我的代码,那么我很感谢你已经检查了所有提供的链接,但我的应用程序出现错误,并说不幸已停止。请给我帮助