WebView 横竖屏遇到的坑

有MainActivity 和 SecondActivity 两个界面 MainActivity强制竖屏,然后跳转到SecondActivity切换到横屏然后返回到MainActivity,SecondActivity的界面onStop()方发调用时间就会有问题,当你快速再次进入的时候会发生先onCreate(),然后才调用上一次的onstop();
发一下测试的代码吧,说起了好乱。

//有Webview的界面,由于横屏效果不好就强制竖屏
public class MainActivity extends AppCompatActivity {


    private WebView mWebView;

    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        mWebView = (WebView) findViewById(R.id.mywebview);
        mWebView = new WebView(getApplicationContext());
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
                startActivity(intent);
            }
        });
    }


}

//这是跳转的界面,从这个界面返回的时候需要是横屏才会有问题(和MainActivity方向不同)
public class SecondActivity extends AppCompatActivity {


    private Button mButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        mButton = (Button) findViewById(R.id.mybtn);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SecondActivity.this.finish();
            }
        });

        Log.d("Second","onCreate");

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("Second","onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("Second","onStop");
    }
}

这个问题在谷歌原生上没问题,在国产手机试了几个都有问题

你可能感兴趣的:(WebView 横竖屏遇到的坑)