完成之前的所有活動

本文翻譯自:Finish all previous activities

My application has the following flow screens : 我的應用程序有以下流程屏幕:

Home->screen 1->screen 2->screen 3->screen 4->screen 5

Now I have a common log out button in each screens 現在我在每個屏幕上都有一個通用的log out按鈕

( Home/ screen 1 / screen 2 /screen 3/ screen 4 / screen 5 ) Home/ screen 1 / screen 2 /screen 3/ screen 4 / screen 5

I want that when user clicks on the log out button(from any screen), all the screens will be finished and a new screen Log in will open . 我希望當用戶點擊退出按鈕(從任何屏幕)時,所有屏幕都將完成,並且將打開一個新的Log in屏幕。

I have tried nearly all FLAG_ACTIVITY to achieve this. 我已經嘗試了幾乎所有FLAG_ACTIVITY來實現這一目標。 I also go through some answers in stackoverflow, but not being able to solve the problem. 我還在stackoverflow中查看了一些答案,但無法解決問題。 My application is on Android 1.6 so not being able to use FLAG_ACTIVITY_CLEAR_TASK 我的應用程序在Android 1.6上,因此無法使用FLAG_ACTIVITY_CLEAR_TASK

Is there any way to solve the issue ? 有什麼方法可以解決這個問題嗎?


#1樓

參考:https://stackoom.com/question/QYmy/完成之前的所有活動


#2樓

When the user wishes to exit all open activities , they should press a button which loads the first Activity that runs when your application starts, clear all the other activities, then have the last remaining activity finish. 當用戶希望退出所有打開的活動時 ,他們應該按下一個按鈕,該按鈕加載應用程序啓動時運行的第一個活動,清除所有其他活動,然後完成最後剩餘的活動。 Have the following code run when the user presses the exit button. 當用戶按下退出按鈕時,運行以下代碼。 In my case, LoginActivity is the first activity in my program to run. 在我的例子中, LoginActivity是我的程序中第一個運行的活動。

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

The above code clears all the activities except for LoginActivity . 上面的代碼清除除LoginActivity之外的所有活動。 Then put the following code inside the LoginActivity 's onCreate(...) , to listen for when LoginActivity is recreated and the 'EXIT' signal was passed: 然後將以下代碼放在LoginActivityonCreate(...) ,以監聽何時重新創建LoginActivity並傳遞'EXIT'信號:

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();  
}

Why is making an exit button in Android so hard? 爲什麼Android中的退出按鈕太難了?

Android tries hard to discourage you from having an "exit" button in your application, because they want the user to never care about whether or not the programs they use are running in the background or not. Android努力阻止您在應用程序中使用“退出”按鈕,因爲他們希望用戶從不關心他們使用的程序是否在後臺運行。

The Android OS developers want your program to be able to survive an unexpected shutdown and power off of the phone, and when the user restarts the program, they pick up right where they left off. Android操作系統開發人員希望您的程序能夠在意外關閉和關閉手機電源後繼續運行,當用戶重新啓動程序時,他們會在他們中斷的地方繼續運行。 So the user can receive a phone call while they use your application, and open maps which requires your application to be freed for more resources. 因此,用戶可以在使用您的應用程序時接聽電話,並打開地圖,這需要釋放您的應用程序以獲取更多資源。

When the user resumes your application, they pick up right where they left off with no interruption. 當用戶恢復您的應用程序時,他們會立即從中斷處繼續操作而不會中斷。 This exit button is usurping power from the activity manager, potentially causing problems with the automatically managed android program life cycle. 此退出按鈕正在從活動管理器中篡奪權力,可能導致自動管理的Android程序生命週期出現問題。


#3樓

You may try Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK . 您可以嘗試Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK It will totally clears all previous activity(s) and start new activity. 它將完全清除所有先前的活動並開始新的活動。


#4樓

From developer.android.com : 來自developer.android.com

 public void finishAffinity () 

Added in API level 16 在API級別16中添加

Finish this activity as well as all activities immediately below it in the current task that have the same affinity. 完成此活動以及當前任務中具有相同親緣關係的緊接其下的所有活動。 This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. 這通常在應用程序可以啓動到另一個任務時使用(例如從它理解的內容類型的ACTION_VIEW),並且用戶已經使用向上導航切換出當前任務並進入其自己的任務。 In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch. 在這種情況下,如果用戶已導航到第二個應用程序的任何其他活動,則應將所有這些活動作爲任務切換的一部分從原始任務中刪除。

Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so. 請注意,此完成不允許您將結果傳遞給上一個活動,如果您嘗試這樣做,則會拋出異常。


#5樓

For logout button on last screen of app, use this code on logout button listener to finish all open previous activities, and your problem is solved. 對於應用程序最後一個屏幕上的註銷按鈕,在註銷按鈕監聽器上使用此代碼完成所有打開的先前活動,您的問題就解決了。

{
Intent intent = new Intent(this, loginScreen.class);
ntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}

#6樓

Intent intent = new Intent(this, classObject);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

This Will work for all Android versions. 這適用於所有Android版本。 Where IntentCompat the class added in Android Support library. IntentCompat在Android支持庫中添加的類。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章