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平台】 Alljoyn学习笔记四 Android(安卓)Core API参考
  2. android 按两次返回键退出
  3. Android教程收集贴
  4. android 9.0 SD卡权限问题 文件管理器没有权限
  5. Android(安卓)Fresco图片处理库用法API英文原文文档2-1(Facebook
  6. android翻译应用、地图轨迹、视频广告、React Native知乎日报、
  7. assets android 获取流 转为string
  8. Android(安卓)Studio 2.0正式版 新特性
  9. android:shape

随机推荐

  1. Android(安卓)多主题切换 (theme + style
  2. Android传感器概述(二)
  3. Android(安卓)-- Parcelable 序列化操作
  4. Android(安卓)UI编程进阶——使用Surface
  5. Android学习小Demo(5)结合Matrix跟Porperty
  6. Android全屏截图的方法,返回Bitmap并且保
  7. Data Binding Guide——google官方文档翻
  8. Android(安卓)内存优化篇 - 使用profile
  9. Android(安卓)基础知识点
  10. 论Android应用进程长存的可行性