How to logout after killing all activities? - android

On my App, I have the following process to log on a user:
The user enters its credentials on the MainActivity and is redirected to HomeActivity using that code for navigation:
Intent accueilIntent = new Intent(getApplicationContext(), HomeActivity.class);
accueilIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
startActivity(accueilIntent);
When the user is on the HomeActivity, he is able to log out thanks to a log out button. Since the event is fired, I would like to kill all activities and redirect the user to the login activity (MainActivity). So The code which is fired on the event is the following:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
The user is well redirected to the MainPage. Then when I press the home button to get back on the Android home screen and then I come back to the App, the right MainActivity appears. However, when I press the native back button to return to the android home screen and then I come back to the App, the HomeActivity appears (the one that should be destroyed before) and not the MainActivity.
Does anyone can tell me why I'm coming back to HomeActivity and not MainActivity in this case?

1] add finish() before startActivity()
2] Add FLAG_ACTIVITY_NO_HISTORY flag.
Intent intent = new Intent(mContext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

If you want user to come back to MainAcitvity when he presses back button on HomeActivity, the parent of HomeActivity should be MainActivity.
In your manifest.xml file, in HomeActivity's tag add this :
<activity
android:name=".HomeActivity"
android:parentActivityName=".MainActivity" >
....
</activity>

Related

Start back an activity that's been killed with finish() function?

After user logs in and goes to second screen(activity), I killed the Login screen activity using finish(). Now that I'am on second screen and I have Logout button there in the action bar, is there a way that login screen which has been killed can be brought back on log out button press ??
You just need to start it again.
startActivity(new Intent(CONTEXT, LoginActivity.class));
Intent intent = new Intent(SecondActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
use this some line when you logout from second activity..

Previous activity stays on activity stack

I'm using this library which is a Wizard.
When user is done with the Wizard and clicks Done button he moves to another Activity.
I have added to Manifest file android:noHistory="true" for the WizardActivity and also when I start the next activity im doing it like this :
Intent intent = new Intent(this, ProjectsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Nevertheless when user click backs he still goes to the WizardActivity. I'd like the user either to exit or go the activity that started the WizardActivity.
Thank you
finish() the activity after launching the other Activity. i.e after startActivity(intent) add finish()

Clearing android stack before starting a new activity

I have an application with three activities: Login, Register and Main activity.
I want to start the Main activity after a user had logged in or registered.
But, when the user presses the 'back' button, he should not see the activity from which he logged in. In other words, I want to clear the activity stack before starting the Main activity.
I saw a few solutions online, for example: this, this and this.
They did not help.
This is my code for launching the main activity from the Login activity:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);
The code in the register activity is quite similar.
I also tried to put finish(); after I started the activity, but it only closed the current activity. Meaning, if I started in the login activity, moved to the register activity and logged in there (Login -> Register -> Main), When I will press the 'back' button I will be return to the Login activity.
Thank you for your help!
Yuval.
The easy way would be to use "noHistory" attribute in manifest. You can set it for the login activity in your manifest file
<activity
android:noHistory="true"
/>
You can do it like this:
If the user goes to the registration screen then just finish the login activity and when they click the back button on their device you can call the onBackPressed () method. In there you can finish the registration screen and start the login activity. If the user went to the registration screen and then registers, you also want to finish the register activity and open the main menu or something else.
Login:
Click on register? Start register activity and finish the login
startActivity (new Intent (Login.this, Register.class));
finish ();
Register:
Click on submit? Start main activity and finish register activity
startActivity (new Intent (Register.this, Main.class));
finish ();
Click om back button?
#Override
public void onBackPressed (){
startActivity (new Intent (Register.this, Login.class));
finish ();
}
And you can also use in the androidmanifest:
android:noHistory="true"
In the activities you want to have no history (where the user can't go to if they click on the back button).
But I prefer to do it in code!
Hope this helps you!
Try this:
Intent intent = new Intent(this, SomeOtherClass.class);
// do not keep this intent in history
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
What you are doing is almost correct, just make a small change in the Intent.
In place of using,
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);
use:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);
It will clear the top, start a new task and clear all the previous task as well. Now, all that Android knows is that there exists a MainActivity in your stack. You can write:
#Override
public void onBackPressed() {
finish();
}
in MainActivity which will allow the user to exit the application and not go back to the LoginActivity.

Android kill all other activities on starting a new one

I have an android activity problem.
Here is how my process works:
Login Activity starts
Login successful. MainMenuActivity starts and LoginActivity is finished by me.
User touched on settings and SettingsActivity starts. MainMenuActivity is NOT finished. because is it the main menu. when user presses the back on settings screen I need to go back MainMenuActivity. so I cant kill MainMenu.
User touched on log out and SettingsActivity is finished by me and Login activity starts. As user returns the login I need to kill MainMenuActivity but I cant.:/
I tried FLAG_ACTIVITY_SINGLE_TOP, CLEAR_TOP, SINGLE_TASK, NEW_TASK, NO_HISTORY etc.. almost all of them didnt work
I put launchMode="singleTask", clearTaskOnLaunh="true" etc. didtn work again.
I tried addFlags() and setFlags() both, didnt work
There are some many issues about this topic here, I read and applied all the suggested solutions and didnt work.
Can anyone help, please?
P.S android:minSdkVersion="8" and android:targetSdkVersion="15" for my app. I didnt use fragments in the app, I use old activity structure.
Use the combination of two flags like this:
Intent intent = new Intent(this, Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
This deletes all the other activities and starts this one.
Try this.
For api level <11
i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
I'd suggest to start the settings activity for result (see here), and when the user requests to logout, set a result accordingly. You will get this result in MainActivity's onActivityResult (see here) and can handle the logout there, finishing the mainActivity before starting the loginActivity.
I suggest that you don't finish the Login activity when you start the main menu. Then you can always clear all activities on logout by doing this:
Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will only work if Login activity is still active at the root (beginning) of the task stack.
To prevent the user from BACKing into the Login activity from the Main activity, you can override onBackPressed() in Main activity and do something else.
Use FLAG_ACTIVITY_CLEAR_TOP like this-
Intent loginIntent = new Intent(this, Login.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
finish();
Intent intent = new Intent(MainActivity.this, MyActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
in this example Mainactivity automatically closed and Myactivity2 Start.
and one thing you need to add finish();

Finish Current Activity and start a new one

I have a Fragment F. After getting result from a service, F needs to finish the activity it is in and launch a page in the web browser which I do using an intent.
It does seem to work fine if the user presses the back button. However, if he launches the app from recent apps, the activity isn't finished.
I have thought about otherways of doing it. Like finishing the current activity and opening the page from the parent activity. But I'll have to make a lot of changes in the flow. So that would be my last option. Is there any way to make sure that the activity is finished even when I launch it from recent apps?
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
getActivity().finish();
Edit: Added code.
try this code for start browser and clear all the stacks of your application
Intent intent = new Intent(); // need to set your Intent View here
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getActivity().startActivity(intent);
getActivity().finish();
Updated
Try this attribute in your activity in AndroidManifiest.xml file
<activity android:name=".MainActivity"
android:excludeFromRecents="true" ...
use the following code
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();

Categories

Resources