开发者

Webview orientation not working

Here is my code. everything works great except the orientation, it reloads every time I rotate the phone it reloads to the home page, it is a pain. can anyone tell me where the problem is.

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.Toast;


public class hpro extends Activity {
/** Called when the activity is first created. */
WebView webview;

//zoom

private static final FrameLayout.LayoutParams ZOOM_PARAMS =

    new FrameLayout.LayoutParams(

    ViewGroup.LayoutParams.FILL_PARENT,

    ViewGroup.LayoutParams.WRAP_CONTENT,

    Gravity.BOTTOM);
//finish zoom

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);




//progress bar

getWindow().requestFeature(Window.FEATURE_PROGRESS);

// frame starts
setContentView(R.layout.main);

webview = (WebView) findViewById(R.id.webview); 
WebSettings webSettings = webview.getSettings(); 
webview.getSettings().setPluginsEnabled(true);
webview.setWebViewClient(new HelloWebViewClient() {}); // MARK
webSettings.setJavaScriptEnabled(true); 



//zoom 
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.webview.getZoomControls();

mContentView.addView(zoom, ZOOM_PARAMS);

zoom.setVisibility(View.GONE);

//end zoom
//loading bar


final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
  public void onProgressChanged(WebView view, int progress) {
    // Activities and WebViews measure progress with different scales.
    // The progress meter will automatically disappear when we reach 100%
    activity.setProgress(progress * 100);
  }
});
webview.setWebViewClient(new WebViewClient() {
  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
  }
});

webview.loadUrl("http://mypage.com/"); 

}

//orientation

@Override
public void onConfigurationChanged(Configuration newConfig){        
   super.onConfigurationC开发者_StackOverflow社区hanged(newConfig);
}


//block browser

private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}


// back key

public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

//start submenu


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}


public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
webview.loadUrl("http://mypage.com");
return true;

case R.id.exit:
// TODO

Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
return true;

case R.id.reg:
webview.loadUrl("http://mypage.com/subpage");
return true;

case R.id.help:
webview.loadUrl("http://mypage.com/help");
return true;

}

return false;
}
}


android:configChanges="orientation" add when u declare activity in android manifest


Your problem is that you are not handling orientation properly, you need to make sure that you override the onSaveInstanceState and onRestoreInstanceState methods in your Activity if you want it to maintain it's state correctly. Have a look at the information in this answer and this answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜