Update: After some exhaustive testing in a wide variety of different scenarios I have come to realise this approach doesn't work in all situations and I am yet to find an elegant solution to handle progress dialogs across orientation change events! There's a bug with Android managed dialogs (pop-ups) that has been annoying me for days. Imagine an application where you need to drill down to some specific content via a list and detail screen. You click an item on the list screen to start a long running connection to the server and the ProgressDialog is initiated, the user then changes the screen orientation between landscape and portrait (with the task running and the dialog showing), the connection finally finishes and the new detail screen (Activity) is started. On the detail screen the user changes orientation again and clicks back. In every instance using managed dialogs the connection pop-up is still running on the list screen even if you have tried to dismiss it before redirecting. I'm a stickler for perfect usability so this has been driving me mad. My solution was to stop using managed dialogs altogether. Instead I created the following 4 methods to replace the default Android way of doing things. Notice the static boolean called isMyDialogShowing, this boolean persists across the entire life cycle of your Activity and is used to determine whether or not to show the dialog. The second part of the puzzle is how to use these methods and the boolean in order to have perfect control of your dialogs across both Activity and orientation changes. In the onCreate() method the first thing I do is call setUpMyDialog(). If the ProgressDialog is null the method instantiates it. The second thing I do is check the static isMyDialogShowing boolean to see if my connection dialog should be showing. If the boolean is false I don't call the showMyDialog() method. If the page has just been loaded for the first time the dialog won't be showing so you will need a user initiated event to start the dialog. In this example I have used a simple button click. When the button is pressed I call my showMyDialog() method for the first time and this displays the previously instantiated dialog and sets the isMyDialogShowing static boolean to true. Every time the page is changed via orientation change or redirection I dismiss the dialog via the onPause() method. Basically that means the dialog is being both shown and dismissed every time the page changes due to an orientation switch. The final ingredient is to call the killMyDialog() method when your connection has finished and you are about to start a new Activity. This sets the isMyDialogShowing boolean to false and ensures that the dialog is never showing when you return to the Activity at a later date.
原文引至:http://www.paxmodept.com/telesto/blogitem.htm?id=766


更多相关文章

  1. Android Fresco图片处理库用法API英文原文文档2-1(Facebook开源An
  2. 原文:Android Theme XML
  3. Android Fresco图片处理库用法API英文原文文档2-2(Facebook开源An
  4. Android Fresco图片处理库用法API英文原文文档4(Facebook开源Andr

随机推荐

  1. 浅析SQL Server 聚焦索引对非聚集索引的
  2. VS2015安装之后加装Sql server2014详细步
  3. 详解SQL Server的简单查询语句
  4. 详解SQL Server数据库架构和对象、定义数
  5. 如何快速删掉SQL Server登录时登录名下拉
  6. 浅述SQL Server的语句类别 数据库范式 系
  7. Android(安卓)View 仿iOS SwitchButton [
  8. Android创建本地服务器笔记
  9. Kotlin初体验
  10. Android(安卓)环境搭建