【问题标题】:Display back button of action bar is not going back in Android在Android中显示操作栏的后退按钮不会返回
【发布时间】:2016-04-06 16:56:54
【问题描述】:

我正在开发一个 Android 应用程序。我正在使用带有 AppCompactActivity 的 ActionBar。在我的应用程序中,我将返回按钮添加到操作栏。但是当我点击它时,它不会回到以前的活动。例如,我从活动 1 开始活动 2。活动 2 包含带有后退按钮的操作栏。但是当我点击活动 2 的操作栏后退按钮时,它不会返回活动 1。

这就是我为活动 2 设置操作栏的方式:

public class EditProfileActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_profile);
        Toolbar toolbar = (Toolbar)findViewById(R.id.profile_action_toolbar);
        setSupportActionBar(toolbar);
        setTitle("Edit Profile");
        ActionBar actionBar= getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

这就是我从活动 1 开始活动 2 的方式:

Intent i = new Intent(MainActivity.this,SecondActivity.class);
                    startActivity(i);

单击此按钮时不会返回

为什么回不去了?

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    将以下内容添加到您的活动中。您必须处理后退按钮的单击事件。

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

    【讨论】:

    • 谢谢。有效。我很快就会把答案变绿。但我已经投了赞成票。
    • 这里是真正的英雄 :)
    【解决方案2】:

    这里有 2 个选项:

    a) 在 AndroidManifest.xml 中为您的SecondActivity 标签提供parentActivityName,如下所示:

     <activity
        ...
        android:name=".SecondActivity"
        android:parentActivityName=".MainActivity" >
    

    b) 像这样在SecondActivity 中覆盖onOptionsItemSelected

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    我建议阅读this guide 了解更多信息。

    【讨论】:

      【解决方案3】:

      我建议不要在 onOptionsItemSelected 中处理“android.R.id.home”,因为它很脆弱。相反,您应该覆盖 onSupportNavigateUp 方法。

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

      注意:如果您使用的是 onOptionsItemSelected ,那么您应该默认返回 false ,否则不会调用 onSupportNavigateUp 方法。

      【讨论】:

      • 你的笔记有帮助。我一直使用 _onSupportNavigateUp _ 并且以前从未同时覆盖 onOptionsItemSelected 和 onSupportNavigateUp 。在 onOptionsItemSelected 作品中返回 false
      【解决方案4】:

      这是你的代码

       public class EditProfileActivity extends AppCompatActivity {
      
              @Override
              protected void onCreate(Bundle savedInstanceState) {
      
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.edit_profile);
                  Toolbar toolbar = (Toolbar)findViewById(R.id.profile_action_toolbar);
                  setSupportActionBar(toolbar);
                  setTitle("Edit Profile");
                  ActionBar actionBar= getSupportActionBar();
                  actionBar.setDisplayHomeAsUpEnabled(true);
              }
      
              @Override
              public boolean onOptionsItemSelected(MenuItem item) {
                  int id = item.getItemId();
                  if (item.getItemId() == android.R.id.home) {
                         finish();
                  }
      
                  return super.onOptionsItemSelected(item);
              }
          }     
      

      【讨论】:

        【解决方案5】:

        将此添加到您的活动中,onCreate()

            Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
        
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        
            toolbar.setNavigationOnClickListener(v -> {
                //What to do when back is clicked
                finish();
            });
        

        【讨论】:

          【解决方案6】:

          首先,请务必查看 Android 指南 http://developer.android.com/intl/pt-br/design/patterns/navigation.html,以防止 Google 屏蔽 Android 应用。

          尝试将此代码添加到您的活动中

          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              return true;
          }
          
          @Override
          public boolean onOptionsItemSelected(MenuItem menuItem) {
              switch (menuItem.getItemId()) {
                  case android.R.id.home:
                      onBackPressed();
                      break;
              }
          
              return super.onOptionsItemSelected(menuItem);
          }
          
          @Override
          public void onBackPressed() {
              super.onBackPressed();
          }
          

          【讨论】:

            【解决方案7】:

            你必须重写 onOptionsItemSelected 并检查项目的 id,如果它与主页按钮的 id 相等,只需调用 onBackPressed 方法。

            @Override
                    public boolean onOptionsItemSelected(MenuItem item) {
                        if (item.getItemId() == android.R.id.home) {
                            onBackPressed();
                        }
                        return super.onOptionsItemSelected(item);
                    }
            

            【讨论】:

              【解决方案8】:

              您必须定义单击该按钮时应该发生的情况,这可以在您的第二个活动的onOptionsItemSelected 方法中完成。请注意 android.R.id.home 常量,它指的是您要使用的活动的后退按钮。

              @Override
              public boolean onOptionsItemSelected(MenuItem item) {
                  switch (item.getItemId()) {
              
                  case android.R.id.home:
              
                      finish(); //close the activty
                      return true;
                  }
                  return super.onOptionsItemSelected(item);
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-03-19
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-07-24
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多