简体   繁体   中英

Open new activity from actionbox menu item and close actually activity

How can i open an activity only one time? If i click more times on the same item in menu it open more times. Is there a way to close the actually viewed activity when open a new one?

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

}

Ok i found the soulion add.Flags to clear the top

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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