How to switch to other activity at orientation change (and then back again)?

六眼飞鱼酱① 提交于 2019-12-24 07:03:24

问题


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));
    }
}

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()

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!


回答1:


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));
    }
}



回答2:


As Sam stated, the super() call should be the first statement in your method. (http://developer.android.com/training/basics/activity-lifecycle/pausing.html)



来源:https://stackoverflow.com/questions/12983792/how-to-switch-to-other-activity-at-orientation-change-and-then-back-again

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!