I'm new to android.I'm building app to listen incoming message over WiFi , it works fine for listening and updating UI but when i try to exit it doesn't respond to back button press until it receive message in buffer. From my understanding, buttons are on UI thread and Runnable is a separate thread and that's why it doesn't respond to button press immediately coz its busy on separate thread. So how can i interrupt "Runnable" from back button press?
Any help would be much appreciated.
public Runnable mUpdate = new Runnable() {
public void run() {
try {
line = in.readLine();
newtext.setText(line);
mHandler.post(this);
Log.i("RESPONSE FROM SERVER", "S: Received Message: '" +line+ "'");
//onBackPressed();
//threadRunning = true;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Error" , "Something Happen");
}
}
};
Edit : Sorry, I should have post this earlier , so in "onCreate" , i use handler to call "mUpdate" . Is it the right way to call or not?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intent = getIntent();
setContentView(R.layout.activity_display_message);
message = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
newtext = (TextView)findViewById(R.id.TextView1);
userName = message[0];
serverIP = message[1];
sendConnectionRequest ();
mHandler = new Handler(); // Handler to update UI
mHandler.post(mUpdate); // post is a method to update UI
}