Back button behavior with tabs and ActivityGroup

后端 未结 2 911
南旧
南旧 2021-01-28 23:29

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

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

    Intent inte         


        
相关标签:
2条回答
  • 2021-01-29 00:01

    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 讨论(0)
  • 2021-01-29 00:04

    Override public void onBackPressed() in your activity and handle the back button click yourself. Calling super.onBackPressed(); will affect the standard behaviour of back button press (also exit your app)

    0 讨论(0)
提交回复
热议问题