Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

android - TextView stops updating after bringing activity back to the front

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...