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.

5 Comments:

  1. Alor Chhota said...
    shimul'da, android-e boss hoye jachchho mone hoy!!
    sabash!!
    Shimul said...
    pari na kisu, beshir vaag jinish mone thake na, ja shikhtesi leikha rakhtesi ;)
    moco said...
    I enjoy reading through your article post, I wanted to write a little comment to support you and wish you a good continuation All the best for all your blogging efforts. Appreciate the recommendation! Let me try it out. Keep working ,great job!
    fotokopi | toko perlengkapan bayi
    Jacinthe said...
    Thanks for taking this opportunity to discuss this, I feel strongly about it and I take pleasure in learning about this topic. If possible, as you gain information, please add to this blog with new information. I have found it extremely useful.
    sewa forklift
    Gerrardz said...
    I enjoy reading through your article post, I wanted to write a little comment to support you and wish you a good continuation All the best for all your blogging efforts. Appreciate the recommendation! Let me try it out for my dijual rumah article blog. Keep working ,great job!

Post a Comment