按下活动操作栏的后退按钮时如何转到上一个选定的选项卡 [英] How to go to previous selected tab when pressing an activity action bar back button

查看:88
本文介绍了按下活动操作栏的后退按钮时如何转到上一个选定的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MainActivity中有六个选项卡,第二个选项卡具有一个列表视图,当用户在列表视图项上按时,它会打开一个带有操作栏的新活动",因此,当用户在第二个活动的后退"按钮上按时,我想要转到主要活动"的上一个标签(第二个标签),但会加载第一个标签"(主页"标签).

I have six Tabs in MainActivity, the second tab have a listview, when user press on the listview item, its open a new Activity with Action bar, so, when user press on the back button of the second activity, I want to go to previous tab (second tab) of Main Activity, but its loading the First Tab (Home Tab).

如何解决此问题?

推荐答案

我们在这里有三种情况.实际的后退按钮(无论是硬件还是软件),操作栏的父按钮(向上")以及两个按钮:

We have three cases here. The actual back button (regardless it is hardware or software), the Action Bar's parent ("Up") button, and both buttons:

  • 后退按钮案例 :

调用SecondActivity时,使用startActivityForResult()使MainActivity随时了解SecondActivity的生命周期.当按下返回"时,在MainActivity.onActivityResult()中捕获此事件并切换到第二个选项卡:

When you call the SecondActivity, use startActivityForResult() to keep MainActivity informed of the SecondActivity's lifecycle. When "back" is pressed, capture this event in MainActivity.onActivityResult() and switch to the second tab:

MainActivity:您当前在哪里开始活动:

// Start SecondActivity that way. REQUEST_CODE_SECONDACTIVITY is a code you define to identify your request
startActivityForResult(new Intent(this, SecondActivity.class), REQUEST_CODE_SECONDACTIVITY);

MainActivity:onActivityResult():

// And this is how you handle its result    
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (requestCode == REQUEST_CODE_SECONDACTIVITY && resultCode == RESULT_CANCEL) {
        switchToTab(2); // switch to tab2 
    }
    // else... other cases
}

  • 操作栏的向上"按钮盒 :
  • 如果必须将此行为连接到操作栏的向上"按钮而不是后退按钮,则必须根据是否使用支持库来覆盖getSupportParentActivityIntent()getParentActivityIntent().

    If this behaviour has to be connected to the Action Bar's "Up" button instead of the back button, you have to override getSupportParentActivityIntent() or getParentActivityIntent() depending on whether you are using the support library or not.

    SecondActivity:get [Support] ParentActivityIntent():

    @Override 
    public Intent getSupportParentActivityIntent() { // getParentActivityIntent() if you are not using the Support Library
        final Bundle bundle = new Bundle();
        final Intent intent = new Intent(this, MainActivity.class);
    
        bundle.putString(SWITCH_TAB, TAB_SECOND); // Both constants are defined in your code
        intent.putExtras(bundle);
    
        return intent;
    } 
    

    然后,您可以在MainActivity.onCreate()中进行处理.

    And then, you can handle this in MainActivity.onCreate().

    MainActivity:onCreate():

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        ...
    
        final Intent intent = getIntent();
    
        if (intent.hasExtra(SWITCH_TAB)) {
            final int tab = intent.getExtras().getInt(SWITCH_TAB);
    
            switchToTab(tab); // switch to tab2 in this example
        }
    
        ...
    

    • 两个按钮盒:
    • 如果您希望以相同的方式处理两个按钮(无论这是一个好主意,还是我不知道),上述两个解决方案都可以并发地实现.

      Should you wish to handle both buttons the same way (regardless this is a good idea or not, I just don't know), both solutions above can be implemented concurrently with no problem.

      旁注:要确定这是否是个好主意,请官方指南可能会有所帮助.尤其是"使用应用程序图标导航"部分.

      Side note: to determine whether this is a good idea or not, this official guide may help. Especially the section "Navigating Up with the App Icon".

      这篇关于按下活动操作栏的后退按钮时如何转到上一个选定的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆