ReadingConfirmationActivity

ReadingConfirmationActivity

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

android - 如何基于某些操作从 Activity 堆栈中删除特定 Activity ?-LMLPHP

在这里,我的第一个活动是ReadingActivity。根据某些操作,用户将被带到NewProjectReadingActivity。在那里,用户将有两个选项。

选项一
选项二

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

选项一
选项二
选项三

如果用户选择了选项一,那么他/她将被带到非常简单的ReadingActivity。我将清除堆栈并再次启动ReadingActivity,如果他/她按下后退按钮,则该应用程序将被最小化,这完全可以。但是,如果用户选择选项2,则他/她将被带到NewProjectReadingActivity。如果我清除活动堆栈并再次启动NewProjectReadingActivity,它将启动NewProjectReadingActivity,但问题是如果用户按下后退按钮,它将使我应用程序,因为它是活动堆栈中唯一的活动。

我想要的是,如果用户在ReadingConfirmationActivity上选择了选项2,那么该用户将被带到NewReadingActivity,这意味着我要删除ReadingConfirmationActivity(这很简单,只需调用finish()),然后在该活动i之前开始该活动, e ReadingInputActivity。

上面提到的每个活动都承载一个片段。我在下面提供活动代码。

阅读活动

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


NewProjectReadingActivity

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


ReadingInputActivity

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


ReadingConfirmationActivity

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

最佳答案

你写了:


  我想要的是,如果用户在
  ReadingConfirmationActivity,则用户将被带到
  NewReadingActivity这意味着我要删除
  ReadingConfirmationActivity(这很容易,只需调用finish()),然后
  活动在该活动之前开始,即ReadingInputActivity


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

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


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

关于android - 如何基于某些操作从 Activity 堆栈中删除特定 Activity ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53705970/

10-10 01:17