【问题标题】:Going back to the previous activity and fragment from an additional back button从附加的后退按钮返回上一个活动和片段
【发布时间】:2015-11-19 13:08:42
【问题描述】:

我有一个在片段之间进行标签导航的应用。其中一个片段具有打开新活动的选项。当我使用此活动中的内置设备后退按钮时,它会返回到选项卡式活动,并选择了上一个片段选项卡。

我在我的应用中的 Activity 的操作栏中添加了一个后退按钮,方法是:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

并在清单中设置父活动,但此按钮始终导航回父活动的第一个选项卡,而不是之前可见的选项卡。

如何使此后退按钮的行为方式与设备后退按钮相同?

【问题讨论】:

    标签: android android-navigation


    【解决方案1】:

    做这样的事情:

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch(id) {
            case android.R.id.home:
                onBackPressed(); 
                return true; 
        } 
        return super.onOptionsItemSelected(item);
    } 
    

    onBackPressed() 方法:

    @Override 
    public void onBackPressed() { 
        super.onBackPressed(); 
    } 
    

    【讨论】:

      【解决方案2】:

      以这种方式处理返回事件

          @Override 
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          switch(id) {
              case android.R.id.home:
                  onBackPressed(); 
                  return true; 
          } 
          return super.onOptionsItemSelected(item);
      }
      

      在背压法中

       @Override 
          public void onBackPressed() { 
           Intent intent = new Intent(SecondActivity.this,TabbedActivity.class);
          intent.putExtra("IsBack",true);
          startActivity(intent);
      }
      

      在您的选项卡式活动中

         protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_tabs);
      
                if(getIntent().getExtras().getBoolean("IsBack")){
                   //navigate to your desire fragment
             }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-25
        • 2020-03-28
        • 1970-01-01
        相关资源
        最近更新 更多