如何根据某些操作从 activity 堆栈中删除特定 activity/activities?

How to remove Specific activity/activities from activity stack based on some action?

我正在开发一个应用程序,我需要根据某些操作导航到特定 activity。这是一张图片

我的第一个 activity 是阅读Activity。基于某些操作,用户将被带到 NewProjectReadingActivity。用户将有两个选择。

选项一
选项二

根据用户选择的选项 he/she 将被带到 ReadingInputActivity。从 ReadingInputActivity 获取输入后,用户将被转发到 ReadingConfirmationActivity。会有三个选项

选项一
选项二
选项三

如果用户选择选项一,那么 he/she 将被带到阅读 Activity,这非常容易。我将清除堆栈并再次启动 ReadingActivity,如果 he/she 按下后退按钮,应用程序将最小化,这完全没问题。但是如果用户选择选项二然后 he/she 将被带到 NewProjectReadingActivity.If 我清除 activity 堆栈并再次启动 NewProjectReadingActivity 然后它将启动 NewProjectReadingActivity 但问题是,如果用户按下后退按钮,它将最小化我的应用程序,因为它是 activity 堆栈中唯一存在的 Activity。

我想要的是,如果用户在 ReadingConfirmationActivity 上选择选项二,那么用户将被带到 NewReadingActivity,这意味着我想删除 ReadingConfirmationActivity (这很简单,只需调用 finish())并且 activity 在 activity 之前开始,即 ReadingInputActivity.

上面提到的每个 Activity 都托管一个片段。我在下面提供 activity 代码。

阅读Activity

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;
    }
}

新项目阅读Activity

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;
    }
}

阅读输入Activity

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;
    }
}

阅读确认Activity

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;
    }
}

您写道:

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.

为此,当用户在 ReadingConfirmationActivity 中选择选项二时,您将执行以下操作:

Intent intent = new Intent(this, NewReadingActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

这将导致顶部(包括)NewReadingActivity 上的所有活动完成。 NewReadingActivity 的新实例将被创建并显示给用户。如果您希望 return 到 NewReadingActivity 的现有实例,您也可以通过将 Intent.FLAG_ACTIVITY_SINGLE_TOP 添加到 Intent.

来实现