Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:15:00+00:00 2026-06-10T11:15:00+00:00

I have an AsyncTask linked to a refresh Button (when I click on my

  • 0

I have an AsyncTask linked to a refresh Button (when I click on my refresh button my AsyncTask is called).

I have on my Layout a LinearLayout field for my ProgressBar :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/grey">
     <Button    android:id="@+id/refresh_p"
                android:text="@string/refresh_promo"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/custom_button1"/>

   <LinearLayout android:id="@+id/linearlayoutProgressBar"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"/>
   <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:listSelector="@color/tabTransparent"
        android:cacheColorHint="#00000000"/>
      <!--   <ListView android:id="@+id/list_promo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>-->
</LinearLayout>

In my AsyncTask :

@Override
    protected void onPreExecute() 
    {
        super.onPreExecute();

        pb = new ProgressBar(context);
        LinearLayout ll = (LinearLayout) ((Activity) context).findViewById(R.id.linearlayoutProgressBar);
        ll.addView(pb);
        pb.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> promoList) 
    {
        ...

        if (pb!=null) 
        {
            pb.setVisibility(View.GONE);
            ((LinearLayout)pb.getParent()).removeView(pb);
        }
    }

The problem I have is when I make more than 2 clicks on my Refresh Button then Multiple ProgressBar are displaying into the screen.. I just want that the new ProgressBar replace the old at the same position ..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:15:01+00:00Added an answer on June 10, 2026 at 11:15 am

    My guess is that you are probably starting your AsyncTask in a manner similar to this:

    new RefreshTask().execute(params);
    

    Instead of that, create an instance variable in your activity. Let’s say this is named mTask and substitute the above code for the following call:

    if(mTask != null && mTask.getState != AsyncTask.State.RUNNING){
        mTask = new RefreshTask();
        mTask.execute(params);
    }
    

    This way you will ensure that only one instance of your task is running at a given time and the user will have to wait until the refresh is finished before he can start a new refresh.

    If you want the user to be able to cancel an existing refresh task and run a new one, you will have to cancel the old one before starting a new one:

    if(mTask != null){
        if(mTask.getStatus() == AsyncTask.Status.RUNNING){
             mTask.cancel(true);
        }
        mTask = new RefreshTask();
        mTask.execute(params);
    }
    

    This way the old task and it’s ProgressBar will be properly replaced by the new one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an AsyncTask which gets called onCreate() in my Main Activity . In
Suppose I have a innerclass extends asynctask called A and the outerclass extends Activity
I have an asynctask that parses an xml file from the net. I'm storing
I have an AsyncTask with an indeterminate ProgressBar that typically executes very quickly, but
In my activity, I have a inner class called A extends AsyncTask . I
I have an AsyncTask which starts on boot (called by a service). In some
I have an AsyncTask and on the background I want to start Service that
I have an AsyncTask with a textview loaded in the class so it looks
My goal is to have an AsyncTask that can execute multiple times (one task
I have been using Asynctask most of time, but in the current app the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.