如何切换到方向变化的其他活动(再回来)? [英] How to switch to other activity at orientation change (and then back again)?

查看:295
本文介绍了如何切换到方向变化的其他活动(再回来)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用我有一个主要活动MyActivity将覆盖onConfigurationChanged()方法。在该方法中我检查的取向的改变,如果改变景观然后调用另一个活动:

In my Android application I have a main activity "MyActivity" which overrides onConfigurationChanged() method. Within that method I check for a change in the orientation, if changed to landscape then I call another activity:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        super.onConfigurationChanged(newConfig);
        startActivity(new Intent(this, BaseFullScreenActivity.class));
    }
}

后来,当我改变我的手机横向的其他活动类BaseFullScreenActivity之称,它工作正常。在那所谓的活动,我再次重写onConfigurationChanged()方法来结束这个孩子再次活动:

Then when I change my mobile to landscape orientation the other activity class "BaseFullScreenActivity" is called, which works fine. Within that called activity I again override the onConfigurationChanged() method to end this child activity again:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        super.onConfigurationChanged(newConfig);
        finish();
    }
}

然而,在这个第二方位的变化(回人像),这应该结束我的孩子的活动,并再次表明主要活动,应用程序崩溃,我收到以下错误:

However, at this second orientation change (back to portrait), which should end my child activity and show the main activity again, the app crashes and I receive the following error:

android.app.SuperNotCalledException: 
Activity MyActivity did not call through to super.onConfigurationChanged()

我没有覆盖的onStop()方法在这两个活动,并呼吁super.onStop(),但是这并不能帮助我。

I did override the onStop() method in both activities and call to super.onStop(), however that did not help me.

任何其他的想法?
在此先感谢您的帮助!

Any other ideas?
Thanks in advance for your help!

推荐答案

为什么不这样做有什么错误提示?如果您将调用父类的if语句之外您的应用程序不会崩溃第二方向变化。

Why not do what the error suggests? If you move the call to the super class outside of your if-statement your app won't crash on the second orientation change.

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        startActivity(new Intent(this, BaseFullScreenActivity.class));
    }
}

这篇关于如何切换到方向变化的其他活动(再回来)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆