Tuesday, January 22, 2008
Did you ever fall in loop when traversing through activities, like one activity launching another and the other launching the previous one, then back pressing returns to that activity. This can be prevented very easily, when you think that the second activity is no more required, you just can call finish() function of that activity. This will solve some problems, though this looping happens only to bad programmers like me :D. Here is an interesting thing, when a subactivity finishes it's job it can send data back to the main activity who started it, for this the main activity has to override a function like this
@Override
protected void onActivityResult(int requestCode, int resultCode,
String data, Bundle extras) {
// TODO Auto-generated method stub
//super.onActivityResult(requestCode, resultCode, data, extras);
if(requestCode == SOME_INTEGER_WHICH_WAS_USED_DURING_CREATION_OF_ACTIVITY_AS_INTEGER_PARAM+OF_INTENT){
String text = "";
// This is a standard resultCode that is sent back if the
// activity doesn't supply an explicit result. It will also
// be returned if the activity failed to launch.
if (resultCode == RESULT_CANCELED) {
text ="Canceled";
// Our protocol with the sending activity is that it will send
// text in 'data' as its result.
} else {
if (data != null) {
text = data;
}
}
}
}
So when finish() function is called from subactivity, this function is called in mainActivity. This code is executed during returning from subactivity class.
setResult(RESULT_OK, "SOMETEXT");
finish();
Most of the codes are copied from APIDemos.
Labels: Activity return, Android
sabash!!
fotokopi | toko perlengkapan bayi
sewa forklift