Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:40:13+00:00 2026-05-30T16:40:13+00:00

Quick question: I have an activitygroup. Within that Activitygroup I have an activity. If

  • 0

Quick question: I have an activitygroup. Within that Activitygroup I have an activity. If I press back while inside this activity. the onBackPressed method of the activity is called – Not the Activitygroups onBackPressed – Why is that ?

EDIT: Got my answer but the problem remains. Here follows code and explanation of my original issue:

I am using ActivityGroups within a TabHost and as such have been “forced” into overriding onBackPressed. I can navigate through my application without issue by pressing back on my phone and by pressing the tabs on my tabhost. But I cannot interact with the interface after pressing Back.
Once I press one of the tabs on the tabhost again I can interact with everything like normal. Why is this happening? Do I need to override onResume?

Relevant code

SettingsActivityGroup :

public class SettingsActivityGroup extends ActivityGroup 
{
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static SettingsActivityGroup group;

// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Allocate history
    this.history = new ArrayList<View>();

    // Set group
    group = this;             

    // Start root (first) activity
    Intent myIntent = new Intent(this, SettingsActivity.class); // Change to the first activity of your ActivityGroup
    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    ReplaceView("SettingsActivity", myIntent);
}

/*
 * Replace the activity with a new activity and add previous one to history
 */
public void ReplaceView(String pId, Intent pIntent)
{
    Window window = getLocalActivityManager().startActivity(pId, pIntent);
    View view = (window != null) ? window.getDecorView() : null;

    // Add the old activity to the history
    history.add(view);

    // Set content view to new activity
    setContentView(view);
}

/*
 * Go back from previous activity or close application if there is no previous activity
 */
public void back()
{
    if(history.size() > 1)
    {
        // Remove previous activity from history
        history.remove(history.size()-1);

        // Go to activity
        View view = history.get(history.size() - 1);
        Activity activity = (Activity) view.getContext();

        // "Hack" used to determine when going back from a previous activity
        // This is not necessary, if you don't need to redraw an activity when going back
        activity.onWindowFocusChanged(true);
        // Set content view to new activity
        setContentView(view);
    }
    else
    {
        // Close the application
        finish();

    }
}
/*
 * Overwrite the back button
 */
@Override
public void onBackPressed() 
{
    // Go one back, if the history is not empty
    // If history is empty, close the application
    SettingsActivityGroup.group.back();

    return;
}
}

Arbitrary child of SettingsActivityGroup(CallForwardActivity)

public class CallForwardActivity extends ListActivity 
{
....
    @Override
    public void onBackPressed() 
    {
        // Go one back, if the history is not empty
        // If history is empty, close the application
        SettingsActivityGroup.group.back();

        return;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:40:15+00:00Added an answer on May 30, 2026 at 4:40 pm

    Because I believe calling onBackPressed() of the currently selected activity is the desired behavior.

    It’s also worth noting that ActivityGroup is deprecated, but I assume you are coding for <3.0 and don’t fancy working with the support libraries.

    Regarding your edited question:
    Another question on this site cites this article as a good ActivityGroup example, and I would agree http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
    This example just calls finish() on the current activity when back is pressed, and lets the os restart the previous activity, which is simpler than what you are doing, and will hopefully work! You can just call getParent() in your child activities too to avoid using that static reference (just seems easier to read to me that way!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick question... I have a query that checks for duplicates that looks like this:
Quick question. I have an app that use a native DLL through PInvoke, this
Quick question. I have an activity that calls my save function at the proper
Quick question, What have I done wrong here. The purpose of this code is
Quick question: I have a very busy form going on that I have developed
I have a quick question. Is it possible to do something like this? The
this is a quick question: I have this Database Project in a Web app
Quick question: If I have a very large function/sub in a class that is
Quick question. I have been Googling this all morning, but it's either not there,
I just have a quick question. I've noticed that I don't have stdafx.h in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.