4

I have an ActivityGroup inside of which I have an Activity. I have overrided the onBackPressed() in this Activity. But unfortunately my onBackPressed() is not being called. So I tried with onKeyDown(). But no effect at all. My problem is, the onBackPressed() event which I have given in the ActivityGroup takes control. How to override the Back Press event inside my sub Activity?

Any help is appreciated.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240

1 Answers1

5

I just had this same problem and I solved it by calling the current activity's onBackPressed from the ActivityGroup:

@Override
public void onBackPressed()
{
    int length = idList.size();
    if (length > 1)
    {
        Activity current = getLocalActivityManager().getActivity(
            idList.get(length - 1));
        current.onBackPressed();
    }
}

idList is a list of activities in the activity group.

Matt Becker
  • 2,338
  • 1
  • 28
  • 36
  • Actually I have used MediaPlayer in my activity. I tried as you have suggested and now I ma able to call my onBackPressed() of my current activity in which I have stopped my MediaPlayer. But still the won't stop playing after returning to my ActivityGroup. – Andro Selva Jun 13 '11 at 07:55
  • Can you detail how you maintain the list of IDs? It seems natural to just call getLocalActivityManager().getCurrentActivity() but that seems to return null. – Artem Russakovskii Aug 09 '11 at 00:58
  • What if you don't want the current activity? I'm using the ActivityGroup to manage a chain of groups. If I have activities A, B, C, D, am currently on activity D and want to go back to B. I have to be able to specify activity B to the .destroyActivity command in LocalActivityManager. The list that I maintain provides this. – Matt Becker Aug 10 '11 at 14:40
  • @ArpitGarg If you are going to mark the answer down, could you at least explain why it didn't work, or give an appropriate solution? – Matt Becker Aug 30 '12 at 18:11
  • @MattBecker I used to call the current activity's onBackPressed from the ActivityGroup: But nothing the problem is same no back press on ActivityGroup was enabled as you said in the post – Arpit Garg Aug 31 '12 at 05:38
  • @ArpitGarg I dunno buddy. I works for me in production code and for the OP. I guess you'll have to work it out yourself. – Matt Becker Aug 31 '12 at 13:25