繁体   English   中英

操作栏的显示后退按钮在 Android 中不会返回

[英]Display back button of action bar is not going back in Android

我正在开发一个 Android 应用程序。 我正在将 ActionBar 与 AppCompactActivity 一起使用。 在我的应用程序中,我将返回按钮添加到操作栏。 但是当我点击它时,它不会回到之前的活动。 例如,我从活动 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);

当我单击此按钮时它不会返回

在此处输入图片说明

为什么回不去了?

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

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

在这里你有 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);
}

我建议阅读本指南以获取更多信息。

我建议不要在 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方法。

这是你的代码

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

将此添加到您的活动中,在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();
    });

首先,请始终参阅 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();
}

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

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

你必须定义当你点击那个按钮时会发生什么,这可以在你的第二个活动的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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM