开发者

How do I return to a calling activity if the user presses BACK?

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 d开发者_如何学Con't know where to put it. Thanks in advance!


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


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...


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();
                    }
                });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜