使用来回按钮从一个活动导航到另一个活动时如何关闭进度对话框?

发布于 2024-12-24 01:12:07 字数 706 浏览 2 评论 0原文

我的 Android 应用程序中有 4 个活动。第一个导致第二个,第二个到第三个等等。每个活动都从网络获取一些数据。这需要一些时间,所以我添加了一个进度对话框来让用户了解它。现在的问题是,当我按后退按钮时,进度对话框仍然存在。我应该在哪里编写miss()函数来关闭进度对话框?

PS:当我按两次后退按钮时它会消失。我应该在 onStart()、onResume() 上写解雇还是在哪里???

这是代码:

private void onFileClick(Option option) {

    ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setProgress(10);
    progressDialog.setMax(100);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setMessage("Loading Data...");
    progressDialog.show();
    Intent i;
        i = new Intent(this, ChannelVideoListActivity.class);   
        startActivity(i);
    progressDialog.dismiss();
} 

I have 4 Activities in my android app. 1st leads to 2nd , 2nd to 3rd and so on.Each activity fetch some data from net.It takes some time in it so i have added a progress dialog box to let users know about it. Now the problem is that when i press the back button the progress dialog is still there. Where should i write the dismiss() function to dismiss that progress dialog box?

PS:It dismisses when i press back button twice. Should i write dismiss on onStart(), onResume() or where ???

Here is the code :

private void onFileClick(Option option) {

    ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setProgress(10);
    progressDialog.setMax(100);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setMessage("Loading Data...");
    progressDialog.show();
    Intent i;
        i = new Intent(this, ChannelVideoListActivity.class);   
        startActivity(i);
    progressDialog.dismiss();
} 

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

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

发布评论

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

评论(4

只有一腔孤勇 2024-12-31 01:12:07

尝试如下所示。

onBackPressed(){
   progressDialog.dismiss();
}

Try like the following.

onBackPressed(){
   progressDialog.dismiss();
}
要走干脆点 2024-12-31 01:12:07

//为此使用异步任务。
拨打此线路

new SomeTask(0).execute();


/** Inner class for implementing progress bar before fetching data **/

 private class SomeTask extends AsyncTask<Void, Void, Integer> 
    {
        private ProgressDialog Dialog = new ProgressDialog(yourActivityClass.this);
        @Override
        protected void onPreExecute()
        {

        Dialog.setMessage("downloading...");
        Dialog.show();
    }

    @Override
    protected Integer doInBackground(Void... params) 
    {
        //Task for doing something 

        return 0;
    }

    @Override
    protected void onPostExecute(Integer result)
        {

        if(result==0)
        {
//do some thing
Intent i = new Intent(this, ChannelVideoListActivity.class);   
        startActivity(i);

        }

// after completed finished the progressbar
        Dialog.dismiss();
    }

//use Async Task for that.
call this line

new SomeTask(0).execute();


/** Inner class for implementing progress bar before fetching data **/

 private class SomeTask extends AsyncTask<Void, Void, Integer> 
    {
        private ProgressDialog Dialog = new ProgressDialog(yourActivityClass.this);
        @Override
        protected void onPreExecute()
        {

        Dialog.setMessage("downloading...");
        Dialog.show();
    }

    @Override
    protected Integer doInBackground(Void... params) 
    {
        //Task for doing something 

        return 0;
    }

    @Override
    protected void onPostExecute(Integer result)
        {

        if(result==0)
        {
//do some thing
Intent i = new Intent(this, ChannelVideoListActivity.class);   
        startActivity(i);

        }

// after completed finished the progressbar
        Dialog.dismiss();
    }
单挑你×的.吻 2024-12-31 01:12:07

我建议您以不同的方式编写 ChannelVideoListActivity

您可能正在调用函数或编写代码来从网络加载数据,然后显示该数据或执行一些数据获取操作。

onCreate() 中删除所有内容,并将其放入 AsyncTaskdoInBackGround() 函数中,并在 AsyncTask 的 onPreExecute 中调用进度对话框,并在 < code>onPostExecute() 关闭对话框。

在 setContentView() 方法调用之后,从 ChannelVideoListActivity 的 onCreate() 调用此 AsyncTask 作为 new TaskClass().execute()

,然后简单地从之前的意图调用 ChannelVideoListActivity 。

I suggest You write your ChannelVideoListActivity differently.

You might be calling function or writing code to load data from web and then show that or doing some data fetching operation.

Remove all from onCreate() and put it in an AsyncTask 's doInBackGround() function and in AsyncTask's onPreExecute call your progress dialog and in onPostExecute() dismiss dialog.

Call this AsyncTask from onCreate() of ChannelVideoListActivity after setContentView() method call as new TaskClass().execute()

and Simply call ChannelVideoListActivity from it's previous with intent.

澜川若宁 2024-12-31 01:12:07

您的呼叫流程存在问题并且进度条消失。

它应该是:

  • 应用程序已启动
  • 活动2已启动
  • 如果有网络调用,则显示进度栏
  • 想要关闭进度栏并完成活动

或者,如代码:

ProgressDialog dialog = new ProgressDialog(this);
    dialog.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            finish();
            return true;
        }
            return false;
        }
    });

PS:在启动新活动之前不要启动进度栏活动。启动活动,然后启动进度栏。

there is a problem in your flow of calling and dismiss of progress bar.

It should be:

  • app started
  • Activity 2 launched
  • If there is network call, show progress bar
  • wanna dismiss the progress bar and finish the activity

Or, as code:

ProgressDialog dialog = new ProgressDialog(this);
    dialog.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            finish();
            return true;
        }
            return false;
        }
    });

PS: DONT start the progress bar before launching the new activity. Launch the activity and then start Progress bar.

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