简体   繁体   English

在开始新活动之前显示进度对话框

[英]Displaying progress dialog before starting a new activity

I have an activity 'Activity1' and also another activity named 'Activity2'. 我有一个活动'Activity1'和另一个名为'Activity2'的活动。 The 'Activity2' is loaded upon clicking a button in 'Activity1'. 单击“Activity1”中的按钮后会加载“Activity2”。 I wanted to display the progress dialog until the new activity is loaded . 我想显示进度对话框,直到加载新活动。 Can you please show me the code to do this 你能告诉我这样做的代码吗?

Display the progress dialog in Activity2's onCreate method, then do all the time-consuming loading in an AsyncTask. 在Activity2的onCreate方法中显示进度对话框,然后在AsyncTask中执行所有耗时的加载。 In the AsyncTask's onPostExecute() method, dismiss the progress dialog. 在AsyncTask的onPostExecute()方法中,关闭进度对话框。

yes by using AsynTask<> you can get your result 是的,使用AsynTask <>你可以得到你的结果

in OnPreExecute Show your Progress dialog,in OndoInBackground run your activity,in onPostExecute remove your dialog 在OnPreExecute中显示你的进度对话框,在OndoInBackground中运行你的活动,在onPostExecute中删除你的对话框

get the idea Get Concept 得到理念获取概念

There is two ways to 有两种方法

First approach To use Async Task 第一种方法使用异步任务

If you are doing heavy tasks eg loading data from server or parsing xml in that case use AsynTask<> If you want to call ActivityB from ActivityA then 如果您正在执行繁重的任务,例如从服务器加载数据或在这种情况下解析xml,请使用AsynTask <>如果要从ActivityA调用ActivityB,则

* step-1 *create a AsyncTask class. * step-1 *创建一个AsyncTask类。 write all background tasks inside doBackground() method and after completion of task you want to call an activity that code write inside onPostExecute() post execute method doBackground()方法中编写所有后台任务,在完成任务后,你要调用一个代码写入onPostExecute()后执行方法的活动

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;



public class LoadingDataFromServer extends AsyncTask {
    Context currentContext = null;

    boolean isCancelled = false;


    public LoadingDataFromServer(Context context) {
        currentContext = context;

    }

    @Override
    protected void onPreExecute() {
        if (DashboardActivity.progressBarLayout != null) {
            DashboardActivity.progressBarLayout.setVisibility(View.VISIBLE);
            // Log.i(TAG,".....Now make progress bar visible.....");
        }

        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(Object... params) {
        // do background processing

        try {
// do background tasks eg sever communication
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object result) {
        // TODO Auto-generated method stub
        // progressDialog.dismiss();

        // call second Activity
        Intent i = new Intent(currentContext, com.ActvityB.class);
        super.onPostExecute(result);
    }

    @Override
    protected void onCancelled() {
        // TODO Auto-generated method stub
        isCancelled = true;
        super.onCancelled();
    }

}

step-2 In the activity fro where you want to jump to new activity (eg in ActivityA) call the execute() of AsynTask 步骤2在要跳转到新活动的活动中(例如在ActivityA中)调用AsynTask的execute()

new LoadingDataFromServer(context).execute(null);

Second approach 第二种方法

  • First show progress dialog. 首先显示进度对话框。
  • create a thread to do all background tasks. 创建一个线程来完成所有后台任务。 when the thread completes the task then cancel the progress dialog and call the next activity 当线程完成任务然后取消进度对话框并调用下一个活动

or 要么

  • when thread complets the task then call next activity pass this object (progress dialog) and inside that new activity dismiss this dialog. 当线程完成任务然后调用下一个活动传递此对象(进度对话框)并在该新活动内部关闭此对话框。

The other answers (using AsynTask) are correct, but the question you need to figure out is what is causing your delay. 其他答案(使用AsynTask)是正确的,但您需要弄清楚的问题是导致延迟的原因。 Is it something happening on the back end of Activity1 or something happening on the front end Activity2. 它是在Activity1的后端发生的事情还是在前端Activity2上发生的事情。 If you're doing some processing before starting Activity2 then follow the advice of Last Warrior or Ted Hopp... if you have some lengthy loading process in Activity2 then you'll need to initiate the progress dialog as the first thing that happens onCreate of Activity2 and then move whatever is taking up processing resources off into an AsynTask (or just another thread) there. 如果您在启动Activity2之前正在进行一些处理,那么请遵循Last Warrior或Ted Hopp的建议......如果您在Activity2中有一些冗长的加载过程,那么您将需要启动进度对话框,因为第一件事就是在创建Activity2然后将处理资源的任何内容移动到AsynTask(或只是另一个线程)。

I guess in the unlikely event that both A1 and A2 are requiring extra time on the end and front of each respectively, you'll need to open and close a progress dialog... I don't think there's a way to keep one open in the foreground as you move from one activity to the other. 我想在不太可能发生的情况下,A1和A2分别需要额外的时间在每个的结尾和前面,你需要打开和关闭一个进度对话框......我认为没有办法保持一个开放状态当你从一个活动移动到另一个活动时,在前台。

you can do it through AsyncTAsk. 你可以通过AsyncTAsk来做到这一点。 Which code taking time for executing just put that into 哪个代码花费时间执行就把它放进去了

doInBackground() doInBackground()

override method of asyncTask and asyncTask的覆盖方法和

startActivity(intent) ----just put into onPostExcute() startActivity(intent)----刚刚放入onPostExcute()

  protected class InitTask extends AsyncTask<Context, Integer, Integer> {

  @Override protected Integer onPreExcute( Context... params )  {

        //assign progressbar here

}
    @Override protected Integer doInBackground( Context... params )  {

         //do all the stuffs here
         return super.doInBackground( params )

} @Override protected void onProgressUpdate(Integer... values)  {
         super.onProgressUpdate(values); 
         //update progress bar
}

    @Override protected void onPostExecute( Integer result )  {
          super.onPostExecute(result);
          //start activity here
 }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM