带有选项卡和 ActivityGroup 的后退按钮行为

发布于 2024-12-01 07:23:54 字数 3848 浏览 4 评论 0原文

我有一个 Activity (Main),它显示如下选项卡:

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,它执行以下操作:

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

因此 Main 有一个带有 MyGroup 的选项卡。 MyGroupActivityA 开始,然后转到 ActivityB。当我按 ActivityB 中的后退按钮时,我想返回到 ActivityA,然后按 ActivityA 中的后退按钮我想关闭应用程序。

我就是这样做的。在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);
    }

最后,在Main中,只是一个系统。 out:

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

现在这是我当前的行为,我不希望这样做:

  • 当应用程序启动时,我位于 ActivityA 中。后退按钮->应用程序关闭。好的。
  • 当应用程序启动时,ActivityA ->活动B ->返回按钮->活动A.好的。
  • 当应用程序启动时,ActivityA ->活动B ->返回->活动A-> 活动B ->返回->应用程序关闭。不行!

显然,第二次显示 ActivityB 时,后退按钮由 Main 处理,而不是 ActivityBMyGroup

我该如何解决这个问题?

编辑

我添加了一个ActivityC。前两个活动中的行为似乎是正常的,从第三个活动开始,Main 活动处理按钮按下。

所以A-> B-> A由A->处理B->主要

和A-> B-> C由A处理-> B->主要的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半边脸i 2024-12-08 07:23:54

在您的 Activity 中覆盖 public void onBackPressed() 并自行处理后退按钮单击。
调用 super.onBackPressed(); 将影响后退按钮按下的标准行为(也会退出您的应用程序)

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)

生来就爱笑 2024-12-08 07:23:54

所以我找到了答案。

我的 Main 中有一个 admob adview,这似乎是导致此问题的原因。这是 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>

当我删除 adview 时,问题不会发生。

现在为了解决这个问题,我将 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">
    <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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文