title: How to remove Specific activity/activities from activity stack based on some action?
body: <p>I am working on an application where i need to navigate to specific activity based on some actions. Here is a image </p>
  
  <p><a href="https://i.stack.imgur.com/mKrkF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mKrkF.png" alt="enter image description here"></a></p>
  
  <p>Here my first activity is the ReadingActivity. Based on some actions user will be taken to the NewProjectReadingActivity. There the user will have Two options.</p>
  
  <p>Option One<br/>
  Option Two</p>
  
  <p>Based on the options chosen by the user he/she will be taken to the ReadingInputActivity. After taking the input from the ReadingInputActivity the user will be forwarded to the ReadingConfirmationActivity. There will be three options</p>
  
  <p>Option One<br/>
  Option Two<br/>
  Option Three<br/></p>
  
  <p>If the user selects the Option One then he/she will be taken to the ReadingActivity which is very easy. I will clear the stack and start the ReadingActivity again and there if he/she presses the back button the app will be minimized which is totally fine. But if the user selects Option Two then he/she will be taken to the NewProjectReadingActivity.If i clear the activity stack and start NewProjectReadingActivity again then it will start the NewProjectReadingActivity but the problem is that if the user presses the back button it will minimize my app as it is the only Activity present in the activity stack. </p>
  
  <p>What i want is that if the user selects Option Two on the ReadingConfirmationActivity then the user will be taken to the NewReadingActivity that means i want to remove ReadingConfirmationActivity (which is easy just call the finish()) and the activity started before that activity i,e ReadingInputActivity.</p>
  
  <p>Every Activity mentioned above is hosting a fragment. I am providing the activity code below. </p>
  
  <p>ReadingActivity</p>
  
  <pre><code>public class ReadingActivity extends BaseAppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if( savedInstanceState == null ) {
              getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, ReadingFragment.getInstance(), ReadingFragment.TAG).commit();
          }
          setDisplayHomeAsUpEnabled(true);
          setActionBarText(getString(R.string.new_project_reading));
      }
  
      @Override
      protected void setupContentView() {
          setContentView(R.layout.activity_reading);
      }
  
      public static Intent newIntent(Context context) {
          Intent intent = new Intent(context, ReadingActivity.class);
          return  intent;
      }
  }
  </code></pre>
  
  <p>NewProjectReadingActivity</p>
  
  <pre><code>public class NewProjectReadingActivity extends BaseAppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if( savedInstanceState == null ) {
              getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, NewProjectReadingFragment.getInstance(), NewProjectReadingFragment.TAG).commit();
          }
          setDisplayHomeAsUpEnabled(true);
          setActionBarText(getString(R.string.new_project_reading));
      }
  
      @Override
      protected void setupContentView() {
          setContentView(R.layout.activity_new_project_reading);
      }
  
      public static Intent newIntent(Context context) {
          Intent intent = new Intent(context, NewProjectReadingActivity.class);
          return  intent;
      }
  }
  </code></pre>
  
  <p>ReadingInputActivity</p>
  
  <pre><code>public class ReadingInputActivity extends BaseAppCompatActivity {
  
      private static final String EXTRA_VALUE_TYPE = "value_type";
      private int valueType = 0;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          valueType = getIntent().getIntExtra(EXTRA_VALUE_TYPE, 0);
          if( savedInstanceState == null ) {
              getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, ReadingInputFragment.getInstance(valueType), ReadingInputFragment.TAG).commit();
          }
          setDisplayHomeAsUpEnabled(true);
          setActionBarText(getString(R.string.reading_input));
      }
  
      @Override
      protected void setupContentView() {
          setContentView(R.layout.activity_reading_input);
      }
  
      public static Intent newIntent(Context context, int valueType) {
          Intent intent = new Intent(context, ReadingInputActivity.class);
          intent.putExtra(EXTRA_VALUE_TYPE, valueType);
          return  intent;
      }
  }
  </code></pre>
  
  <p>ReadingConfirmationActivity</p>
  
  <pre><code>public class ReadingConfirmationActivity   extends BaseAppCompatActivity {
  
      private static final String EXTRA_VALUE_TYPE = "value_type";
      private static final String EXTRA_READING_VALUE = "reading_value";
      private int valueType = 0;
      private double readingValue = 0;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          valueType = getIntent().getIntExtra(EXTRA_VALUE_TYPE, 0);
          readingValue = getIntent().getDoubleExtra(EXTRA_READING_VALUE, 0);
          if( savedInstanceState == null ) {
              getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, ReadingConfirmationFragment.getInstance(valueType, readingValue), ReadingConfirmationFragment.TAG).commit();
          }
          setDisplayHomeAsUpEnabled(true);
          setActionBarText(getString(R.string.reading_input));
      }
  
      @Override
      protected void setupContentView() {
          setContentView(R.layout.activity_reading_confirmation);
      }
  
      public static Intent newIntent(Context context, int valueType, double readingValue) {
          Intent intent = new Intent(context, ReadingConfirmationActivity.class);
          intent.putExtra(EXTRA_VALUE_TYPE, valueType);
          intent.putExtra(EXTRA_READING_VALUE, readingValue);
          return  intent;
      }
  }
  </code></pre>