Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Intent Flag to destroy activity and start new one

Tags:

So I have a Login Activity This Activity inflates a login.xml layout which has a USER_NAME and PASSWORD EditText Views, when I enter the Username and Password and click the Login Button I start a new Activity.

The new Activity has a Logout button which basically just starts the previous Activity like so:

    Intent loginIntent = new Intent(getActivity(), Login.class);     loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);     getActivity().startActivity(loginIntent); 

According to the Android Documentation the flag does the following:

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

The problem is that the Username and Password still appear in the EditText Views after I logout, is there a Flag that destroys the Login activity and just starts a new one or is there a way to reset the fields whenever I logout? Which is the better approach?

like image 719
Eric Bergman Avatar asked Nov 05 '13 20:11

Eric Bergman


People also ask

How do I use intent to launch a new activity?

The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i);

How pass data from one intent to another intent in Android?

Using Intents This example demonstrate about How to send data from one activity to another in Android using intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is the method to kill or terminate an activity in Android?

exit() the system will indeed kill the current process and remove the activity from the back stack (in this case SecondActivity ) and it will also resume the activity that it's below on the stack — which in this case will be from the same application since there's no call to finish() on our code.


1 Answers

You have 2 choices:

1 - Kill the login activity after a successful login

Intent loginIntent = new Intent(getActivity(), Login.class);     loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);     getActivity().startActivity(loginIntent); finish(); 

2 - Empty the values then start new activity

edittext_username.setText(""); edittext_password.setText(""); 
like image 111
Noob Avatar answered Oct 01 '22 02:10

Noob