views:

60

answers:

3

My MAIN activity is spawning a child activity that contains a ListView. While this ListView is being populated (through an AsyncTask), an indeterminate progress bar is shown.

However, assuming that I am an impatient user and I press the BACK button, the progress bar is cancelled but I am left with a blank screen. I have to press BACK one more time to go back to the MAIN activity.

I would like the app to go back directly to the MAIN activity by pressing BACK only once. Can somebody point me in the right direction? I am thinking I should call finish() somewhere but I don't know where to put it. Thanks in advance!

A: 

you can try like this,place this code in your activities oncreate block.

findViewById(R.id.back).setOnClickListener(new OnClickListener() { public void onClick(View v) {

                    finish();
                }
            });
MGSenthil
I am not using an icon as a back button though, I was referring to the BACK button on the handset in my question.
Zarah
A: 

Use setOnCancelListener() on Dialog (which ProgressDialog extends).

alexanderblom
Just what I was looking for! Thanks! :)
Zarah
what actually it's doing,suppose i'm in new activity where i use the this function.
MGSenthil
Hi @MGSenthil, can you explain more? I don't think I understand your question.
Zarah
A: 

you should override "onBackPressed()" it will call when the activity has detected the user's press of the back key.

@override
 public void onBackPressed(){
   this.startActivity(this,MainActivity.class);
 }

this code will call the MainActivity when emulator/ipphone back button pressed...

Kandhasamy