为什么使用android:configChanges是一个坏习惯 [英] why using android:configChanges is a bad practice

查看:823
本文介绍了为什么使用android:configChanges是一个坏习惯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理过有关轮换和AsyncTask的不同帖子和问题.在每篇文章中都提到使用android:configChanges 这是一个坏习惯.但是我没有发现为什么不鼓励它,以及为什么它不是一个好习惯的真正原因.如果使用android:configChanges来处理方向,有什么缺点.

I have gone through different posts and questions for handling rotation and AsyncTask. In each post it is mentioned that using android:configChanges is a bad practice. But i didn't find the actual reason why it is discouraged and why it is a bad practice. What are the disadvantage if we use android:configChanges to handle orientation.

注意: 我知道它已经回答了如何处理方向和AsyncTask.但是我想知道不使用android:configChanges的原因.

Note: I know it is already answered how to handle orientation and AsyncTask. but I want to know reason behind not using android:configChanges.

推荐答案

好吧,您需要记住Activity可以出于多种原因而重新启动.

Well, you need to remember that an Activity can be restarted for multiple reasons.

例如,这些原因之一是当您的应用程序在后台运行时,操作系统决定将其杀死(当然是用Activity)以回收内存.

For example, one of these reasons is when your app is in the background and the OS decides to kill it (with your Activity, of course) to reclaim memory.

当您返回应用程序时,操作系统将尝试在离开时重新创建您的Activity,但是由于您决定不打扰它,因此仅尝试在清单中使用android:configChanges即可,但操作失败.

When you return to your app, the OS will try to recreate your Activity as you left it, but will fail to do so, because you decided not to bother with it, just used android:configChanges in your Manifest.

如果确保您的应用程序可以从重新启动正常恢复,则可能完全没有必要android:configChanges.因此,需要使用android:configChanges可能表明您的应用存在某些缺陷,值得一看.

If you make sure your app can recover properly from a restart, android:configChanges might not be necessary at all. Because of this, the need to use android:configChanges might indicate some flaw in your app, that may worth to take a look at.

使用android:configChanges并不是一种不好的做法,但是如果您不完全了解自己在做什么,那很容易做到.

It's not bad practice to use android:configChanges, but it pretty easily can be, if you don't understand exactly what you're doing.

这篇关于为什么使用android:configChanges是一个坏习惯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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