当定位在Android的改变活动重载 [英] Activity reloads when orientation changes in Android

查看:98
本文介绍了当定位在Android的改变活动重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要活动启动画面后启动时,它解析4大XML文件。这需要几秒钟,我能够隐藏这时候的启动画面显示在启动时。

When my main activity starts after the splash screen it parses 4 big XML files. This takes a few seconds and I'm able to hide this in the time the splash screen shows at startup.

但是,当屏幕方向改变或当我回去的主要活动的子活动就会再次解析前我的应用程序'挂'了几秒钟,它不响应或在任何时间之后。

But when the screen orientation changes or when I go back to the main activity after a sub-activity it parses again and thus fore my app 'hangs' for a few seconds and it doesn't respond or anything in that time.

我明白这是Android的正常行为,而不是有没有办法避免这种情况?

I understand this is normal behaviour in Android, but isn't there a way to avoid this?

我看了一些关于savedinstancestate,但我似乎无法理解的文档。

I read something about savedinstancestate, but I can't seem to understand the docs..

推荐答案

添加此code在你的Andr​​oid清单文件中的每个活动节点,如果你的目标API级别小于或等于12。

add this code in your android manifest file in every activity node if you're targeting API level 12 or less.

android:configChanges="orientation|keyboardHidden"

13级以后,你还必须有屏幕尺寸,即:

For level 13 and onwards you also have to include "screensize", i.e.:

    android:configChanges="orientation|keyboardHidden|screenSize"

现在粘贴到您的每一次活动的Java类此code。

now paste this code in your every activity's java classes.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}

这篇关于当定位在Android的改变活动重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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