views:

53

answers:

4

I'm using code similar to whats below with the exception that the thread is started via a button press. When I press the button, the data at the url is grabbed and it switches me over to the other activity. The issue i'm having is when I'm at the other activity and hit the backbutton I get an Exception saying that the thread has already been started. I've tried killing the thread when the activity loads checking for isAlive but it won't seem to die. Does anyone happen to know how I would get this activity back to it's original state with the created thread not running?

import java.io.BufferedInputStream;  
import java.io.InputStream;  
import java.net.URL;  
import java.net.URLConnection;  
import org.apache.http.util.ByteArrayBuffer;  

public class Iconic extends Activity {  
private String html = "";  
private Handler mHandler;  

public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
    mHandler = new Handler();  
    checkUpdate.start();  
}  

private Thread checkUpdate = new Thread() {  
    public void run() {  
        try {  
            URL updateURL = new URL("http://iconic.4feets.com/update");  
            URLConnection conn = updateURL.openConnection();  
            InputStream is = conn.getInputStream();  
            BufferedInputStream bis = new BufferedInputStream(is);  
            ByteArrayBuffer baf = new ByteArrayBuffer(50);  

            int current = 0;  
            while((current = bis.read()) != -1){  
                baf.append((byte)current);  
            }  

            /* Convert the Bytes read to a String. */  
            html = new String(baf.toByteArray());  
            mHandler.post(showUpdate);  
        } catch (Exception e) {  
        }  
    }  
};  

private Runnable showUpdate = new Runnable(){  
    public void run(){  
        Intent newIntent = New Intent(Iconic.this, otherClass.class);
        startActivity(newIntent);
    }  
};  

}

A: 

I had a similar problem with one of my applications: I was doing something on a different thread, and while the thread was running the user would switch to landscape mode creating a new activity. Once the thread finished the application would force close because the thread was pointing to an activity that no longer existed.

My solution to this problem was to use AsyncTask. Basically it abstracts a thread and a handler, and allows you to get periodical progress updates on your background task (ie. implement something like a ProgressBar). I also called AsyncTask.cancel() from my Activity.onDestroy() method so when the activity was destroyed, the AsyncTask would stop running.

Jwsonic
A: 

In this case you don't have to span up a new thread for starting up an activity . .rather

do this

{ in your spaned thread.

progressHandler.sendMessage(progressHandler.obtainMessage());

}

Handler progressHandler = new Handler() { public void handleMessage(final Message msg) {

        Intent newIntent = New Intent(Iconic.this, otherClass.class);
    startActivity(newIntent);

   }
};
success_anil
A: 

You can't call .start() twice on the same Thread instance. Have you tried to recreate it?

...

checkUpdate = new Thread(checkUpdateRunnable).start();

...

private Runnable checkUpdateRunnable = new Runnable() {  
    public void run() {

...

Hope that helps, bye!

lencinhaus
A: 

You should run the thread in a service. This video (from Google I/O 2010) is pretty good for a perspective on how the Android activity lifecycle and a service in application architecture was designed.

DrBloodmoney