从动作框菜单项打开新活动,然后关闭实际活动

时间:2018-10-21 23:32:47

标签: java android-activity menu

我怎么只能打开一个活动?如果我在菜单中的同一项目上单击更多次,则会打开更多次。打开新的活动时,是否可以关闭实际查看的活动?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    //menu item clickd
    if (id == R.id.share) {
            startActivity(new Intent(MainActivity.this, HowToUse.class));
        return true;
    }

    if (id == R.id.terms) {
        Intent intent = new Intent(this,InfoAbout.class);
        this.startActivity(intent);
        return true;
    }

    if (id == R.id.howuse) {
        Intent intent = new Intent(this,HowToUse.class);
        this.startActivity(intent);
        return true;
    }

    return  super.onOptionsItemSelected(item);

}

1 个答案:

答案 0 :(得分:0)

好吧,我找到了灵魂乐团add.Flags清除顶部

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    //menu item clickd
    if (id == R.id.share) {
            startActivity(new Intent(MainActivity.this, HowToUse.class));
        return true;
    }

    if (id == R.id.terms) {
        Intent intent = new Intent(this,InfoAbout.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    if (id == R.id.howuse) {
        Intent intent = new Intent(this,HowToUse.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    return  super.onOptionsItemSelected(item);

}