安卓返回键无响应

11

我正在尝试手动实现当操作栏上的向上按钮被按下时必须执行的操作,但出于某种原因,当我按下它时什么也没有发生。

这是我的代码:

public class ActivityOne extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_one);
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Button button = (Button)findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openActivityTwo();
            }
        });
        Button button2 = (Button)findViewById(R.id.btn2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openActivityThree();
            }
        });
    }


    void openActivityTwo(){
        Intent intent = new Intent(this, ActivityTwo.class);
        startActivity(intent);
    }

    void openActivityThree(){
        Intent intent = new Intent(this, ActivityThree.class);
        startActivity(intent);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_activity_one, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        else if(id == R.id.homeAsUp){
            Log.i("","Up is pressed");
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我知道必须在清单文件中显式指定一个父活动,以便为我想要实现向上导航的活动分配父级,但问题是这个活动有多个父级,因此我认为在按下此活动上的向上按钮时调用finish()方法会是更好的方法。

我已经尝试了id == R.id.homeid == R.id.homeAsUp,它们都不起作用。我不知道是否因为我正在使用AppCompactActivity或其他原因。请帮帮我。


你正在按钮点击事件中启动另一个活动,而不是在actionButtonBack点击事件中。 - Arun Shankar
3个回答

22

以下是实现方法,尝试此代码

使用 android.R.id.home 替代 R.id.home 或 R.id.homeAsUp

public boolean onOptionsItemSelected(final MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                //use onBackPressed() OR finish();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

1
有一天我会给你买杯子蛋糕,非常感谢你! - spongyboss

3
正确的方法是覆盖 public boolean onNavigateUp(),就像覆盖 onBackPressed() 一样。

我已经实现了onNavigateUp()、onItemSelected()并添加了.setNavigationOnClickListener(),但无论如何都无法返回到上一个活动。 - filthy_wizard
@filthy_wizard 你尝试过调用 onBackPressed() 吗? - hrishikesh

0

使用Android Studio 3.6.2,我发现我的应用程序默认情况下不显示“向上”按钮,但可以轻松添加。

转到AndroidManifest.xml并按以下方式更新需要“向上”按钮的每个活动:

<activity
    android:name=".yourClassName"
    android:parentActivityName=".MainActivity">
    <meta-data android:name="android.support.PARENT_ACTIVITY" 
    android:value="com.example.yourPackageName" />
</activity>

通过更改parentActivityName设置Up按钮的目标位置。在此示例中,我将其设置为“.MainActivity”,但您可以将其更改为要让用户导航到的任何活动。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接