1

I have Main Activity. That has 4 tabs (TabHost). I have overridden onBackPress() in MainActvity, as well as all four activities. This button show user a dialog box and for conformation of Exit When app start. It show first tab. Then if I press back it works fine.

But if I go for next 3 tab and then press back, the app stop. OnDestroy() of Main is called. But there is not dialogue for the user.Even noting is print in log cat. That I have written in onBackPressed() method From and of 5 activities including MainActivity.

I have also try onKeyDown() for back key, but the result is same. Why is that the case?

halfer
  • 19,824
  • 17
  • 99
  • 186
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105
  • Show us a simple example of your code **simple** – Blundell Jun 16 '11 at 08:55
  • Sorry, I have now use GroupActivity for for customizing tab. Thank. Now its working perfectly. – Arslan Anwar Jul 28 '11 at 06:08
  • 1
    You shouldn't just say 'oh now it's working bye' You should explain your decision in an answer maybe with some code or a link too what you've changed to as your reasoning. 1) You can then mark this as the correct answer and keep your stats up 2) If anyone else coems over this question they have a lead to follow – Blundell Jul 28 '11 at 08:52

3 Answers3

1

I come to know it was difficult to open new activity inside the previous tabs when I am using TabHost. I google it and found GroupActivity is the best solution for this problem. GroupActvity Example But GroupActivity have the same problem when open new activity in the previous tab. the back button not work properly for new activity. After search I found that was due to the focus was alwasys on parent activity. I have to make

setFocusable(true); requestFocus();

on my new activity component to gain focus.

I am now using GroupActivity for customizing Tabbar and activities.As I ma also maintaining stack of activity ids in parent activity so that I can pop out the recent activity when user press back button.

else if you are NOT going to implement Activity focus then you should maintain stack in parent and when press the back button it will initiate parent onBackPressed(); and you can call the child onBackPressed() functionality as discussed in the link.

onBackPressed() not working inside ActivityGroup

Community
  • 1
  • 1
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105
0

well one thing i know for sure is that you will always get the onBackPressed() called in the MainActity if you are running with a tabHost and not in the child views. The only thing that comes to mind is if you have consumed the event in the onBackPressed method (return true) because if you didnt it will go and still follow the default process and destroy your activity.

DArkO
  • 15,880
  • 12
  • 60
  • 88
  • Thanks for time. But there should be some log printing... that say. Back button pressed. I am on ABC activity. But I not getting this to. But only that log is "I am destroying" child activities and the parent activities.? – Arslan Anwar Jun 16 '11 at 08:37
  • 1
    well i have done this in many of my apps and as i said the tab activity will always handle the key events, same goes for menu items. so unless you are doing something really odd then the back press will be registered. without some code and structure i can't provide any more info. but your focus is definitely on the tab activity to register the keys. maybe try to override 1 by 1 (first only tab activity that will return true and once that will return false) then comment out the tab activity if that doesn't work and try for the children. then experiment with the returns. – DArkO Jun 16 '11 at 11:23
0

I meet this problem,but i have shot it now. @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK) { showExitDialog(); return true; }
return super.onKeyDown(keyCode, event); }

public void showExitDialog()
{
    new AlertDialog.Builder(this)
    .setTitle("Attention")
    .setMessage("Do you want to exit this application")
    .setPositiveButton("YES", exitListener)
    .setNegativeButton("No", cancelListener)
    .show();
}
at the first time i lost a "reture true" in onkeydown()
xiaodouya
  • 87
  • 1
  • 11