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 8215863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:55:44+00:00 2026-06-07T11:55:44+00:00

Somehow I cant use the GUI before the AsyncTask finished. I didn’t recognize any

  • 0

Somehow I cant use the GUI before the AsyncTask finished. I didn’t recognize any difference between posting the execute() in “onCreate” or “onCreateOptionsMenu” method.
Do you know what’s wrong?
Thanks for any help.

package com.test11;

import java.util.concurrent.ExecutionException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class TestActitivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_actitivity);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_test_actitivity, menu);

        try
        {
            int t = new TestTask().execute().get();
        }

        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        catch (ExecutionException e)
        {
            e.printStackTrace();
        }

        return true;
    }

    class TestTask extends AsyncTask<Void, Void, Integer>
    {
        public Integer doInBackground(Void...voids)
        {
            try
            {
                Thread.sleep(10000);
            }

            catch(Exception e)
            {
                e.printStackTrace();
            }

            return 1;
        }
    }
}
  • 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-07T11:55:46+00:00Added an answer on June 7, 2026 at 11:55 am

    You execute your task like this:

    new TestTask().execute().get();
    

    What do you do exactly here? You create a new task instance. Then you run it. And then you call get() on your task, which waits until the computation of this task is completed to return it’s value. Which means that onCreate() or onCreateOptionsMenu() are blocked until the second thread is finished. If you remove get(), your task won’t block the UI.

    If you want to work with your result, you should override onPostExecute() in your AsyncTask instead of using get(). onPostExecute() runs in the UI thread and allows you to use the result to manipulate the UI easily. If you want to display your result in a textview for example, you would use an implementation like this:

    @Override
    public void onPostExecute(Integer result) {
        TextView tv = (TextView) findViewById(R.id.sampletext);
        tv.setText("Result was " + result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somehow my code doesn't work any more (it did work before with the exact
Somehow my Properties window has become detached. When I view properties of any item
somehow I get never any results when I call: select * from table_1 t1
I cant get the form_widget (from mopabootstrapbundle) to use the translation (from FOSUserBundle) to
There are like 1000 posts on this, but somehow I cant get things to
this seems like very simple maths but somehow, my brain cant think ... i
Where can I get an algorithm to render filled triangles? Edit3: I cant use
Can I use the typeid / type_info somehow to detect whether some type is
I've been working all day and I somehow can't get this probably easy task
I'm busy implementing the wurfl api using PHP, but somehow just can't seem to

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.