Ad

Avoid Opening Activity Over Again If The Activity Is Already Opened

- 1 answer

I opened camera activity using menu bar. But after it opens if i click the menu bar it opens on top of it. And when i press back button it shows camera activity twice.

Is there any flag i can use?

@Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

@Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); 
        mScannerView.startCamera();         
    }

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();          
}

@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
         if (item.getItemId() == R.id.nav_barcode) {
            Intent i = new Intent(this, Scanner.class);
            startActivityForResult(i, 1);
        } 
}
Ad

Answer

Use flag Intent.FLAG_ACTIVITY_SINGLE_TOP when starting activity.

Ad
source: stackoverflow.com
Ad