显示进度对话框直到加载新活动

发布于 2024-12-19 00:56:58 字数 1486 浏览 1 评论 0原文

当用户单击按钮启动新活动时,我试图显示一个进度对话框。微调器应显示在当前页面上,直到出现其他活动。 (该活动有时可能需要 4-5 秒才能启动,如果没有微调器,它只会显示一个看起来冻结的按下的按钮)

这就是我所拥有的,只有删除 hideProgressDialog();,但显然,当我回到上一个活动时,旋转器仍然在那里。

我做错了什么?

进度对话框:

    public void showProgressDialog(Context context) {
    if(this.progressDialog != null) {
        this.progressDialog.dismiss();
        this.progressDialog = null;
    }
    this.progressDialog = ProgressDialog.show(context, "", "Chargement en cours, veuillez patienter");
}    

public void hideProgressDialog() {
    if(this.progressDialog != null) {
        this.progressDialog.dismiss();
        this.progressDialog = null;
    }
}

函数:

public void startActivity(Context context, Class<? extends Activity> activityClass) {
    try {
        showProgressDialog(context);
            Intent intent = new Intent(this, activityClass);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         
            startActivity(intent);    
            hideProgressDialog();
    }
    catch(Throwable e) {
        e.printStackTrace();
    }
}  

单击按钮的示例,其中调用函数以显示微调器:

    @Override
public void onClick(View view) {
    if(view.getId() == R.id.changeBannerButton) {
        getBaseApplication().startActivity(this, BannerListActivity.class);
    }...

I have a progress dialog I am trying to show when a user clicks a button to launch a new activity. The spinner should be displayed on the current page until the other activity appears. ( The activity can take sometimes up to 4-5 seconds to launch and without the spinner it just shows a pressed button that looks frozen )

This is what I have, it's only working if I remove hideProgressDialog();, but then the spinner will still be there when I back to the previous activity, obviously.

What am I doing wrong ?

Progress Dialog :

    public void showProgressDialog(Context context) {
    if(this.progressDialog != null) {
        this.progressDialog.dismiss();
        this.progressDialog = null;
    }
    this.progressDialog = ProgressDialog.show(context, "", "Chargement en cours, veuillez patienter");
}    

public void hideProgressDialog() {
    if(this.progressDialog != null) {
        this.progressDialog.dismiss();
        this.progressDialog = null;
    }
}

Function :

public void startActivity(Context context, Class<? extends Activity> activityClass) {
    try {
        showProgressDialog(context);
            Intent intent = new Intent(this, activityClass);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         
            startActivity(intent);    
            hideProgressDialog();
    }
    catch(Throwable e) {
        e.printStackTrace();
    }
}  

Example of a button click where this calls the function to show the spinner :

    @Override
public void onClick(View view) {
    if(view.getId() == R.id.changeBannerButton) {
        getBaseApplication().startActivity(this, BannerListActivity.class);
    }...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在巴黎塔顶看东京樱花 2024-12-26 00:56:59

onResume() 方法中调用 hideProgressDialog()。这样,如果用户按下后退按钮,onResume() 方法就会被调用并立即隐藏进度对话框。

Call hideProgressDialog() in the onResume() method. This way, if the user presses the back button, the onResume() method gets called and immediately hides the progress dialog.

恍梦境° 2024-12-26 00:56:59

首先,您应该将新的 DialogFragment 类与 FragmentManager 一起使用。因为 showdialog() 从 API 级别 8 开始已被弃用,

接下来您应该使用 showdialogremovedialog 来添加和删除对话框。

您应该使用onCreateDialog来处理对话框和操作。就像在显示进度对话框时启动一个新线程来运行该作业一样。

Well First off you should use the new DialogFragment class with FragmentManager. Because showdialog() is deprecated from API level 8

Next you should use showdialog and removedialog for adding and removing the dialog.

And you should use the onCreateDialog to handle the dialog and the operations. Like start a new thread to run do the job when you are displaying the progressdialog.

谜泪 2024-12-26 00:56:59

尝试在活动开始时在单独的线程中加载数据。但在该过程开始之前会显示进度对话框。现在,一旦该过程完成,请使用 runOnUI 隐藏进度对话框。

指示用户正在加载数据

http://www.helloandroid.com/tutorials/using-threads-and-progressdialog

和这个:

Android - 使用 runOnUiThread 从线程进行 UI 更改

Try to load data in a seperate thread on start of activity. But before start of that process show the progress dialog. Now once the process is done use runOnUI to hide the progress dialog..

Indicate the user that data is being loading

http://www.helloandroid.com/tutorials/using-threads-and-progressdialog

and this:

Android - using runOnUiThread to do UI changes from a thread

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文