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

  • SEARCH
  • Home
  • 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 7865749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:10:53+00:00 2026-06-03T00:10:53+00:00

I am new to programming and I have struggled with this for days and

  • 0

I am new to programming and I have struggled with this for days and cannot get it to work.

I have a loop to execute some tasks. It will display a picture in an ImageView for that particular task. So it goes something like this:

  • setImageResource
  • do some task
  • wait for 1 second
  • then start everything again until the commands end
  • when all commands end, close that activity by calling finish()

I tried to use a handler with runnable and asynctask but I cannot get it to work. The UI is always updated later than the 1 second delay. As I call finish() after the commands ends, the photo seems not show up at all.

for(int i=0; i<actions.size(); i++) {
            switch((int)actions.get(i)[0]) {
            case 0:
                ivActionIcon.setImageResource(R.drawable.image);
                sleeping=true;
                DelayAsyncTask delay=new DelayAsyncTask();
                delay.execute();
                while(sleeping){};
                break;
            }
        }
        finish();
class DelayAsyncTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... params) {
        try {
            Thread.sleep(5000);
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    @Override 
    protected void onPostExecute(Bitmap bitmap) {
        sleeping=false;
    }

}

I am using a variable “sleeping” to let the main thread to wait for the delay.

  • 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-03T00:10:54+00:00Added an answer on June 3, 2026 at 12:10 am

    Just from taking a guess this loop is somewhere in a UI thread. That means that you are blocking the UI thread until the async finishes. That is a NO-NO! Why not just update the image in the onPostExecute? Something like

    class DelayAsyncTask extends AsyncTask<String, Void, Bitmap> {
        @Override
        protected Bitmap doInBackground(String... params) {
            try {
                Thread.sleep(5000);
            } catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }
        @Override 
        protected void onPostExecute(Bitmap bitmap) {
            ivActionIcon.setImageResource(R.drawable.new_image);
        }
    

    obviously remove this line of code as well (I don’t think I ever know a reason for doing something like that anyway).

    while(sleeping){};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am new kernel programming.i have been trying to load this driver program for
I'm pretty new in iphone programming and have stumbled upon this issue which I
I'm new to funcctional programming and have some questions regarding coding style and debugging.
I'm new to programming and have some questions about classes. I'll use Karel as
I am new to computer programming and have some experience programming with python. I
I am new to programming.I have seen this code.returning a derived class object to
I am pretty new to programming and have absorbed some 150 pages of a
I'm pretty new to Python programming so I have this question: How can I
I'm still new to C# programming and have been able to get WebDriver working
I'm new to programming and have taken some classes in it so I'm not

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.