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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:48:22+00:00 2026-06-14T07:48:22+00:00

Again, another question about ayncTask. Is the what I am doing below correct? Class

  • 0

Again, another question about ayncTask. Is the what I am doing below correct?

Class UpdatePersonActivity{

   Person person;
.
.
.
.
.    

    private class UpdatePersonAsyncTask extends AsyncTask<Void, Void, Void> {

        private ProgressDialog dialog;
        private DBHandler dbHandler;


        @Override
        protected void onPreExecute() {

            dialog = ProgressDialog.show(UpdatePersonActivity.this, "Please wait..", "working..", true);  
            dbHandler = new DBHandler(UpdatePersonActivity.this);
        }

        @Override
        protected Void doInBackground(Void... params) {
            dbHandler.open();
            long id = dbHandler.updatePerson(person);
                        person.setId(id);
            dbHandler.close();

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            dialog.dismiss(); 
            Toast.makeText(UpdatePersonActivity.this, "Tenant "+person.getName()+" has been updated successfully!", Toast.LENGTH_SHORT).show();
            finish();

        }
    }

Basically I have “person” variable which is in the activity class and the same variable is used to insert into DB and updating its ID in the DoInBackground, and the same variable is used for GUI purpose

Can I do that? I tried it and it works but is it something I shouldnt do?

Thanks

  • 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-14T07:48:24+00:00Added an answer on June 14, 2026 at 7:48 am

    As far as sharing a variable between an AsyncTask and the UI thread, the only concern is synchronization. If it is possible that the UI thread (or any other thread) might be updating your Person object while it is being accessed in doInBackground, then you need to synchronize or otherwise coordinate access. If during the time the AsyncTask executes you are guaranteed that the only accesses to the shared Person object will not modify the object, then you can dispense with the synchronization.

    However, you should be aware of a general problem with using AsyncTask as an inner class of an Activity. If your activity is restarted for any reason (for example, a configuration change such as the user rotating the phone), then the activity that the AsyncTask updates will no longer be valid. A description of the problem and some suggestions for what to do about it are described here.

    See here for even more discussion on this issue, including an approach for maintaining a progress dialog for an AsyncTask across activity restarts.

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

Sidebar

Related Questions

I have another question again about visual basic, the language I am using now
Sorry to bother You with yet another question about K&R programs. But again some
I had another question about this issue, but I didn't ask properly, so here
This question about unit tests sparked another thing that's been bothering me. I've gone
My question is about VMWare. I read I can use VMWare to get another
I posted another question earlier about getting the arguments from a context menu App.
again a total newbie question from me about Haskell, and Leskah. (First, a subjective
Again I have a question about an ssh issue: On a embedded system (no
I'm busy getting confused about non-deterministic destruction. In an answer to another question I
I have a question about the correct way to handle errors in VBA in

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.