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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:40:19+00:00 2026-06-11T10:40:19+00:00

I’m trying to figure out threading and have this issue where a TextView stops

  • 0

I’m trying to figure out threading and have this issue where a TextView stops being updated if the app is sent to the back, and then restored.

How can I ensure that the TextView continues to be updated after the app is brought back to the front?

Or…

How do I reconnect the TextView to the handler in my run-nable thread after restarting the activity?

There is a Progress Bar which works just fine, so I’m somewhat confused. I’d appreciate some advice as I think I may be making a simple mistake.

public class ThreadTestActivity extends Activity {
    private Handler handler;
    private static ProgressBar progress;
    private TextView tv;
    private int counter = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ThreadTest);
        handler = new Handler();
        progress = (ProgressBar) findViewById(R.id.progressBar1);
        tv = (TextView) findViewById(R.id.myText);
        Button but = (Button) findViewById(R.id.Button01);

        but.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                thread_fun();
            });}
    }
    private void thread_fun() {

        new Thread(new Runnable() {
            public void run() {

                try {
                    while (counter < 100) {
                        counter += 20;
                        Thread.sleep(2000);
                        // Update the progress bar and TextView (in 5 chunks of 20, 0 to 100) 
                        // This works perfectly it the app stays in front
                        handler.post(new Runnable() {
                            public void run() {
                            // but after sending to the back (esc) and bringing the activity back to the front
                                progress.setProgress(counter); //This progres bar maintains its value and updates correctly
                                tv.setText(String.valueOf(counter)); //This TextView reverts to its default text and does not update  
                            }
                        });
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
  • 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-11T10:40:21+00:00Added an answer on June 11, 2026 at 10:40 am

    When an Activity is backgrounded and then resumed, it goes through the whole lifecycle. This means a new instance is created, and Activity.onCreate is called again. See the diagram here for details. An important thing to note is that Activities go through this lifecycle and are destroyed and re-created also when the orientation is changed. So it’s something you kind of have to be aware of and work with or around.

    The reason that your ProgressBar is updated when your Activity is backgrounded and resumed is that you’ve got a static reference in your Activity that is set upon each call to onCreate. This static reference is shared between the first instance of your Activity and the second instance that is created after you resume. Once you start your thread, it will just update whatever ProgressBar this reference points to, so you get the impression that the same ProgressBar is being updated.

    In fact, it’s not the same object, it’s a different instance. That’s why the text in your TextView is not updated, because the thread is updating an older instance of TextView, not the one on the screen. You can confirm all this in the debugger, or more simply just by printing out the hash codes of the objects. Try putting this inside your while loop:

    Log.d("thread_fun", "threadid="+Thread.currentThread().getId()+
        ",progressbar="+progress.hashCode()+
        ",textview="+tv.hashCode());
    

    Notice that the ProgressBar’s hash code changes, because the thread is updating a new ProgressBar, but the hash code of the TextView that is being updated does not.

    D/thread_fun(28989): thread=3483,progress=1097438768,textview=1097437160
    D/thread_fun(28989): thread=3483,progress=1097438768,textview=1097437160
    D/thread_fun(28989): thread=3483,progress=1097528568,textview=1097437160
    D/thread_fun(28989): thread=3483,progress=1097528568,textview=1097437160
    

    Now, the answer is not to make the TextView also static. This isn’t how you should maintain state between the Activity lifecycle changes, because it can lead to memory leaks if you’re not careful, and frankly it’s also confusing, as you can see from above. Instead, you should override one of the other lifecycle methods, onPause or onStop, to save the state of the ProgressBar and TextView, and probably kill the thread that is updating it. Then, in onCreate, you’ll need to pull that state out of the Bundle and restore the ProgressBar and TextView to the state they were when the user navigated away, and also probably restart the thread. The method you probably want to read up on is called Activity.onSaveInstanceState. There are other options too, but this is something that’s covered on StackOverflow in a lot of questions and is described well in the Android SDK docs.

    I hope that helps! Let me know if this isn’t clear!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.