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 6359181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:29:05+00:00 2026-05-24T23:29:05+00:00

I have an Activity ( Main ) which shows tabs like this: private void

  • 0

I have an Activity (Main) which shows tabs like this:

private void initTabs(){
    mTabHost = getTabHost(); // The activity TabHost

    Intent intent;
    intent = new Intent().setClass(this, MyGroup.class);
    setupTab(intent, "tab");
}

private void setupTab(Intent intent, final String tag) {
    View tabview = createTabView(mTabHost.getContext(), tag);
    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
    mTabHost.addTab(setContent);
}

private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);

    ((TextView) view.findViewById(R.id.tabsText)).setText(text);
    return view;
}

The MyGroup class is an ActivityGroup, which does the following:

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

    View view = getLocalActivityManager().startActivity("activityA", new Intent(this, ActivityA.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    this.setContentView(view);

            state = A;
}

public void openB() {
    Intent i = new Intent(this, ActivityB.class);

    View view = getLocalActivityManager().startActivity("activityB", i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    this.setContentView(view);
            state = B;
}

So Main has a tab with a MyGroup. The MyGroup starts with ActivityA and then goes to ActivityB. When I press the back button in ActivityB, I want to go back to ActivityA, and on back press in ActivityA I want to close the app.

This is how I’ve done that. In ActivityB:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    System.out.println("ActivityB PRESSED");
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        MyGroup mg = (MyGroup) getParent();
        return mg.onKeyDown(keyCode, event);
    }
    return super.onKeyDown(keyCode, event);
}

In ActivityA the same:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        System.out.println("ActivityA PRESSED");
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            MyGroup mg = (MyGroup) getParent();
            return mg.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }

Now, in MyGroup:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            System.out.println("MG PRESSED");   
                if (state == A)
                return false;
            else if (state == B) {
            setContentView(getLocalActivityManager().startActivity("activityA", new Intent(this, ActivityA.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
                state = A;
                return true;
        }

        return false;
        }
        return super.onKeyDown(keyCode, event);
    }

Finally, in Main, just a system.out:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    System.out.println("MAIN PRESSED");
    return super.onKeyDown(keyCode, event);
}

Now this is my current behavior, which I don’t want:

  • When app started, I’m in ActivityA. Backbutton -> app closes. OK.
  • When app started, ActivityA -> ActivityB -> Back button -> ActivityA. OK.
  • When app started, ActivityA -> ActivityB -> Back -> ActivityA ->
    ActivityB -> Back -> app closes. NOT OK!

Apparently, the second time ActivityB is shown, the back button is being handled by Main, instead of ActivityB or MyGroup.

How can I fix this?

EDIT

I’ve added an ActivityC. It seems like behaviour in the first two activities is normal, and from the third on, the Main activity handles the button press.

So A -> B -> A is handled by A -> B -> Main

and A -> B -> C is handled by A -> B -> Main.

  • 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-24T23:29:05+00:00Added an answer on May 24, 2026 at 11:29 pm

    So I found the answer.

    I have an admob adview in my Main, which seems the cause of this problem. This is the xml:

        <?xml version="1.0" encoding="utf-8"?>
        <TabHost
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
            android:id="@android:id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"    
                        android:padding="0dp">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true" />
            <com.google.ads.AdView
                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ads:adUnitId="@string/adkey"
                ads:adSize="BANNER"
                ads:loadAdOnCreate="false"
                android:layout_alignParentBottom="true" 
                android:focusable="false"/>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_above="@id/adView"
                android:layout_below="@android:id/tabs" />
    
        </RelativeLayout>
    </TabHost>
    

    When I remove the adview, the problem doesn’t occur.

    Now to solve this, I’ve changed the xml into this:

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="0dp">
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true" />
    
                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_below="@android:id/tabs" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <com.google.ads.AdView
                    android:id="@+id/adView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    ads:adUnitId="@string/adkey"
                    ads:adSize="BANNER"
                    ads:loadAdOnCreate="false"
                    android:layout_alignParentBottom="true"
                    android:focusable="false" />
            </RelativeLayout>
        </FrameLayout>
    </TabHost>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following in my main activity: private void attemptLogin() { this.runOnUiThread(new Runnable()
I have a main activity which calls a child one via Intent I =
I have a JSON file which is populated to an activity (Main.java). This Activity
I have a ListView on main activity, which shows a Contact picture on the
Say if I have a locationManager(LM) object in activity A, which is my main
I have an activity which shows some List entries. When I click on a
I have a Activity called main. If I call Toast.makeText(this, Hello World from main,
I have an activity 'A' which is called from Main activity every second until
suppose that you have two activities A(which is the main activity) and B when
I have an activity which shows 3 random images from JSON data off the

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.