Finishing Previous Activities on Logout
In an Android application navigating through multiple screens (Home to Screen 5), the requirement is to implement a logout button on each screen that, when clicked, terminates all previous activities and opens a Login screen.
The FLAG_ACTIVITY_CLEAR_TASK flag, which would have been an ideal solution, is unavailable in Android 1.6. However, there is an alternative approach:
Create an Intent for the Home screen activity:
<code class="java">Intent intent = new Intent(getApplicationContext(), Home.class);</code>
Add the FLAG_ACTIVITY_CLEAR_TOP flag to the Intent:
<code class="java">intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);</code>
Start the Activity:
<code class="java">startActivity(intent);</code>
The FLAG_ACTIVITY_CLEAR_TOP ensures that all activities above the Home screen are cleared, effectively returning the user to the Home screen.
If the Login screen needs to be launched directly, you can include an extra in the Intent and check for it in the Home screen activity. Depending on the specific scenario, you may need to finish the Home screen activity as well.
The above is the detailed content of How to Implement Logout and Clear Previous Activities in Android 1.6?. For more information, please follow other related articles on the PHP Chinese website!