本文介绍了有标签和的ActivityGroup后退按钮行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个活动(),这表明标签是这样的:

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;
}

MYGROUP 类是一个的ActivityGroup,它执行以下操作:

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;
}

所以有一个 MYGROUP 标签。该 MYGROUP 开始 ActivityA 键,然后进入 ActivityB 。当我美元的 ActivityB 点$ PSS后退按钮,我想回去 ActivityA ,并在回$在 ActivityA 点$ PSS我想关闭该应用程序。

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.

这是我是如何做到这一点。在 ActivityB

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);
}

ActivityA 相同的:

@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);
    }

现在,在 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);
    }

最后,在,只是一个System.out的:

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:

  • 当应用程序启动后,我在ActivityA。后退按钮 - >应用程序关闭。确定。
  • 当应用程序启动后,ActivityA - > ActivityB - >后退按钮 - > ActivityA。确定。
  • 当应用程序启动后,ActivityA - > ActivityB - >返回 - > ActivityA - >ActivityB - >返回 - >应用程序关闭。也不行!
  • 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!

显然,第二次ActivityB显示,后退按钮是由,而不是 ActivityB 处理或 MYGROUP

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?

修改

我添加了一个ActivityC。这似乎是在头两个活动的行为是正常的,从第三上的活动处理的按钮preSS。

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.

所以,A - > B - > A是被处理 - > B - >主

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

和A - > B - > C由一个处理 - > B - >主

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

推荐答案

于是,我找到了答案。

我在主要,这似乎是这个问题的原因是AdMob的AD浏览报。这是XML:

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>

当我删除AD浏览报,不会出现问题。

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

现在要解决这个问题,我已经将XML变成这样:

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>

这篇关于有标签和的ActivityGroup后退按钮行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:14