There is a main activity and the menu from which 3 more activities are called. In all 4 activities there is a code:

@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_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_about) { Intent intent = new Intent(this, AboutActivity.class); startActivity(intent); } if (id == R.id.action_add) { Intent intent = new Intent(this, ActivityAdd.class); startActivity(intent); } if (id == R.id.action_edit) { Intent intent = new Intent(this, ActivityEdit.class); startActivity(intent); } return super.onOptionsItemSelected(item); } 
  • What do you mean by correct completion? - Maxim Kuznetsov
  • In general, you do not need to specifically complete the activity, the system will decide this question itself. But if you really want to, just add finish(); after each call startActivity(); - pavlofff
  • I launch the application and switch to the AboutActivity activity, from there to the ActivityAdd, then back to AboutActivity, then back to the ActivityAdd and all this through the menu. And when I click back on the phone, these activities are closed as expected. And I would like to make it so that the activity was opened only once from whatever activity it would cause). Suppose we are on the AboutActivity activity and call ActivityAdd from it, so that the activity of AboutActivity is destroyed - Victoria
  • then finish (); use - Maxim Kuznetsov
  • I correctly understand what is needed in the onOptionsItemSelected method when calling another activation to call this.finish (); - Victoria

1 answer 1

first option

Hardcore

finish();

Option Two in AndroidManifest.xml

 <activity android:name=".YourActivity" android:noHistory="true" /> 

third option in YourActivity.java

 intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 

or

 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
  • Thanks, finish (); helped. - Victoria