簡體   English   中英

在當前活動上單擊按鈕(不是后退按鈕)時關閉當前和上一個活動

[英]Closing current and previous activity on click of button(not the back button) on current activity

我從主要活動 A 開始。從 AI go 到活動 B,從 BI go 到 C。 現在在活動 C 中,如果單擊按鈕(不是后退按鈕),活動 B 和 C 需要關閉,並且需要創建活動 B 的新實例。 但是,如果未按下該特定按鈕並且按下返回按鈕,則活動 C 將關閉,我們將返回上一個活動 B。我正在制作一個提醒應用程序。 活動 B 是展示所有任務的回收器視圖,活動 c 是創建新任務。 因此,如果在活動 C 上按下提交按鈕,我希望關閉活動 C 和 B 並打開一個新的 b 實例,即帶有新任務的回收器視圖,但如果未按下提交,我想關閉活動 Z0D61Z0B8370CAD1D3E412F78 B. 怎么做?

也許你可以使用這兩種方法

(1) 使用 IntentReceiver

從第二個活動開始

  Intent i = new Intent(SecondActivity.this, ThirdActivity.class);
    i.putExtra("SecondActivity", new ResultReceiver(null) {
        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            SecondActivity.this.finish();
        }
    });
    startActivityForResult(i, 1);

在第三個活動

   ((ResultReceiver) Objects.requireNonNull(getIntent().getParcelableExtra("MainActivity"))).send(1, new Bundle());

    //And after this finish current activity (Second activty)

    startActivity(new Intent(ThirdActivity.this, FirstActivity.class));
    finish();

(2) 如果清除所有以前和當前的活動

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

您可以使用方法finishAffinity()退出活動。 我希望這至少使問題變得容易一些。

使用意圖標志 FLAG_ACTIVITY_CLEAR_TOP 啟動活動 B

https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM