7

If i start a background thread, what will happen if the activity that is started from finishes() before the thread terminates. Will the thread terminate as well or will it stay alive?

 new Thread(new Runnable() {
                public void run() {
                    while (mProgressStatus > 0) {


                        // Update the progress bar
                        mHandler.post(new Runnable() {
                            public void run() {
                                progressbar.setProgress(mProgressStatus);
                            }
                        });
                    }
                }
            }).start();
Parvaz Bhaskar
  • 1,367
  • 9
  • 29
Jake
  • 2,877
  • 8
  • 43
  • 62

2 Answers2

2

Threads run idependently from their parents. Thread dies when it returns from Thread.run() back to JVM normally or due to an uncaught exception.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
-2

For what you know your thread/application can die at any point, any time, a phone crashes to the ground and the battery is off and the last thing you know it's a phone that it's turned off in the quickest possible way.

The Android lifecycle and its management it's complicated and there is no real answer because at Google, when commenting this sort of things, they like the word "automagic" a lot, so they are surely not giving away any internal detail about this, at least not in "plain old english".

There are the first Google IO about Android that talks about this stuff, you can go back on youtube and search for a Google IO talks about the Android lifecycle if you want more details about this.

Anyway keep in mind that Android grants you absolutely nothing about how and how long your app will live, and you have at least to variables to consider: what the OS ( Android ) is doing and what the user is doing, and this 2 things can even be mixed togheter when the user gives an input involving the life of your app that Android needs to handle.

user2311177
  • 252
  • 1
  • 4