避免在android中更改方向时使用异步任务重新加载活动 [英] Avoid reloading activity with asynctask on orientation change in android

查看:21
本文介绍了避免在android中更改方向时使用异步任务重新加载活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户改变方向时,如何避免在 android 中使用 asynctask 类重新加载 Activity?谁能举个例子?

How do I avoid reloading the activity with asynctask class in activity when the user changes orientation, in android? Can anybody give an example?

推荐答案

Chirag 已经正确地指出了解决方案.

Chirag have already correctly pointed out the solution.

我只想补充一点,Activity的重启与其中的AsyncTask无关.默认情况下,当有任何配置更改时,除非 Activity 明确处理配置更改,否则活动将重新启动.这是通过在清单文件中创建一个条目来完成的.

I just want to add that, restart of Activity has nothing to do with the AsyncTask in it. By default, when there are any config changes,the activity is restarted UNLESS the config change is explicitly handled by the Activity. This is done by making an entry in manifest file.

浏览此链接开发人员指南:android:configChanges

Go through this link developer guide : android:configChanges

注意:检查设备上而非模拟器上的代码.因为我之前也遇到过类似的问题.相同的代码在模拟器上不起作用,但在设备上完美运行.因此,在清单中添加 android:configChanges 条目,并在 android 设备而不是模拟器上检查应用程序.

NOTE: Check the code on a device not on emulator. Because I had faced similar problem earlier. The same code didn't worked on emulator but it worked on device perfectly. So add the entry for android:configChanges in manifest and check the application on a android device instead of emulator.

我第二个@hackbod.只在必要时才做!

And I second @hackbod. Do it only if necessary!

这篇关于避免在android中更改方向时使用异步任务重新加载活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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