页面(Activity)之间的平滑跳转及封装
来源:互联网 发布:直播平台源码 开源 编辑:程序博客网 时间:2024/05/17 23:42
Android应用开发中,往往包括多个界面(Activity),共同构成一个应用程序。然而,页面之间经常需要跳转,在跳转的过程中有以下几个常见的问题:
1、跳转的界面,即第二个Activity的执行将消耗一定的系统资源和时间,在这之前页面不会显示(黑屏)。对于资源消耗少的页面尚可接受,但是对于大量资源消耗而导致的长时间黑屏,将大大降低用户的友好性。
2、为了增加用户的友好性,一种可行的办法是使用异步任务(AsyncTask)和等待提示相结合,告诉用户系统正在运行。但是如何封装一个公用的模式,供应用程序使用呢?
本文将通过一个例子来说明。其中核心的思想时异步任务,即AsyncTask启动一个背景Thread, 并在工作完成的时候将控制权交还给UI Thread. 示例:
经过以上两个步骤之后,封装基本结束,最后自要在Activity中简单调用即可,如下面的片段代码:
1、跳转的界面,即第二个Activity的执行将消耗一定的系统资源和时间,在这之前页面不会显示(黑屏)。对于资源消耗少的页面尚可接受,但是对于大量资源消耗而导致的长时间黑屏,将大大降低用户的友好性。
2、为了增加用户的友好性,一种可行的办法是使用异步任务(AsyncTask)和等待提示相结合,告诉用户系统正在运行。但是如何封装一个公用的模式,供应用程序使用呢?
本文将通过一个例子来说明。其中核心的思想时异步任务,即AsyncTask启动一个背景Thread, 并在工作完成的时候将控制权交还给UI Thread. 示例:
首先定义一个后台执行和UI更新的运行接口,Excution.java
public interface Excution {void longExcute();void uiUpdate();}
longExcute()方法定义了长时间的后台操作,uiUpdate()方法定义了后台操作完成后的调用方法,以便更新UI主界面。
其次,继承AsyncTask对象,构建LongOperation对象,构造子Excution对象从 Activity传入。在需要执行后台长时间操作时回调longExcute()方法,后台操作完成后回调uiUpdate()方法。而在后台执行的过程 中,系统的提示筐被封装在ProcessDialog对象里。
import android.app.ProgressDialog;import android.content.Context;import android.os.AsyncTask;import android.util.Log;public class LongOperation extends AsyncTask<String, Void, String>{private static final String TAG = "LongOperation";private ProgressDialog dialog; private Context context;private Excution excution; private boolean showDialog = true;public LongOperation(Context context,final Excution excution){this.context = context;this.excution = excution;showDialog = true;}public LongOperation setShowDialog(boolean showDialog){this.showDialog = showDialog;return this;}@Overrideprotected String doInBackground(String... params) {// perform long running operation operationLog.d(TAG,"doInBackground()");excution.longExcute(); return null;}@Overrideprotected void onPostExecute(String result) {// execution of result of Long time consuming operationLog.d(TAG,"onPostExecute()");try{ if(showDialog){ dialog.dismiss(); } dialog = null; excution.uiUpdate(); }catch(Exception e){ }}@Overrideprotected void onPreExecute() {// Things to be done before execution of long running operation. For example showing ProgessDialogLog.d(TAG,"onPreExecute()");if(showDialog){dialog = new ProgressDialog(context); dialog.setMessage("Loading"); dialog.setTitle("Please wait..."); dialog.show();} }@Overrideprotected void onProgressUpdate(Void... values) {// Things to be done while execution of long running operation is in progress. For example updating ProgessDialogLog.d(TAG,"onProgressUpdate()"); }public interface Excution {void longExcute();void uiUpdate();}}
经过以上两个步骤之后,封装基本结束,最后自要在Activity中简单调用即可,如下面的片段代码:
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);new LongOperation(this, new Excution() {@Overridepublic void longExcute() {//get data}@Overridepublic void uiUpdate() {setContentView(R.layout.userinfo);//set data to UI}}).execute(new String[] {});}
扩展及思考:
1、读者可以根据自身需要修改弹出筐样式,或者自定义View,如果有多种样式,这可以通过构造子传入。
2、如果不需要弹出筐,通过设置setShowDialog(false)方法即可完成,此在页面不跳转,但是又需要后台操作,同时不用阻塞UI时尤其有用
- 页面(Activity)之间的平滑跳转及封装
- 封装activity之间跳转时的方法.
- activity之间的跳转及传递数据
- Activity及Fragment之间的跳转
- activity之间的跳转(activity传参)
- 封装类中实现另外两个Activity之间的跳转
- activity跳转的封装
- Activity 之间的跳转
- activity之间的跳转
- Activity之间的跳转
- activity之间的跳转
- Activity之间的跳转
- Activity之间的跳转
- Activity之间的跳转及传值,并证明finish不是彻底销毁Activity(AndroidStudio)
- Android--Activity之间的跳转(Intent)
- 对Activity跳转的封装
- 实现2个Activity之间跳转及数据的传递
- fragment与activity及两个fragment之间的跳转实现
- android换肤功能实现(apk方式)
- DeDeCMS v5.7最新利用工具[一键暴密码]0day
- ext中xtype对照表
- Jquery获取元素的父容器对象
- ubutun 下 Pygments的安装
- 页面(Activity)之间的平滑跳转及封装
- iPhone开发之隐藏系统状态栏
- 单例
- 4.OD-调试示例1
- VC中引用第三方库,常见的库冲突问题
- 第一次使用FolderBrowserDialog
- 一句话木马新方法
- wcf学习参考博客
- 《算法导论》 第7章 快速排序