Clear android activity stack starting a new activity - android

I have an application and every new created activity will start an async task to validate the user session. If the session is valid, the application flows continues. If not, the whole activity stack must be cleared and there should be only the login activity. This activity has a "no history" flag so it is never kept in the stack.
I've been trying some solutions provided here: Android: Clear Activity Stack but with no success.
This must works on the lowest android possible, being the least 2.2
Thanks!

I keep my login Activity on the stack. In the onResume() of the login Activity, I check to see if the user has login credentials and, if so, call startActivity for the next screen presented after login. The user does not see the login screen in this case.
When the user presses the logout button, I clear the user's credentials and then this clears the stack all the way back to the login screen:
Intent intentLaunchLogin = new Intent(this, ActivityLogin.class);
intentLaunchLogin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentLaunchLogin);
Also, if the user is on the screen presented after the login and they press the 'back' button, I don't want them to go to the login Activity. This code will send the user to the Home screen as would be expected:
moveTaskToBack(true);

Could you do something like is described here:
http://blog.janjonas.net/2010-12-20/android-development-restart-application-programmatically
basically you create an alarm that starts your intent, then you close your app completely.

This is what I always do and works perfectly.
I start the app with the main activity an check if the user is logged in, if he is not logged in launch the login activity like this
void launchLoginActivity(){
/* Move user to LoginActivity, and remove the backstack */
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
It will not allow u to go back

Related

Android prevent to go back to the previous Activity

How can prevent when user click back, only back to one page before the login Activity. If hit the last page before login Activity, then exit the app. Now, if i click go back it will show the login page and need login again. Once user login, unless they click logout button, otherwise don't show login Activity.
Any idea on this?
Thank you.
If you would like to simply prevent the back button to navigate back to your login Activity, you could just set the android:noHistory attribute to true for your login Activity in your Manifest.
Something like this:
<activity
android:name=".LoginActivity"
android:noHistory="true" />
Always open your activity after splash screen. Decide which activity you want to navigate the user using the stored login data. To store login data use shared preferences. But at the time of login, after logging in if user clicks back, user will be navigated back to login activity. So once the user successfully logged in clear the activities on stack while navigating the user to main/home activity
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
use shared preference to achieve this:
On Login successful, write a shared preference like below
SharedPreferences settings = getSharedPreferences("your_preference_name", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("LoggedIn", true);
editor.commit();
Also to prevent other activities to come back to Login page, we should finish the login page before moving to target screen
Intent intent = new Intent();
intent.setClass(this, TargetClass.class);
startActivity(intent);
this.finish();
Also whenever u come back to the application, u can directly move ur user to Home page by this
SharedPreferences settings = getSharedPreferences("your_preference_name", 0);
boolean isLoggedIn = settings.getBoolean("LoggedIn", false);
if(isLoggedIn )
{
//Go directly to Homescreen.
}
when you start an activity from Login Activity then after StartActicity(), finish the login activity by calling finish() function.
call finish() in your login activity when opening the next activity after user logs in. And as someone pointed out that you can store your authtoken in sharedPreference once user logs in and use that data to maintain the session.
You can do it by this way too, via playing with the launchmode of the android manifest.
<activity
android:name=".yourPackageName.LoginActivity"
android:launchMode="singleTop"/>
when you do login and Intent than call in login Activity
finish();
In Kotlin, this is how to go about it:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Intent(this, MainActivity::class.java)
startActivity(intent)
finish()

clearing all open previous activities not working in android 5.0

My app has a log in MainActivity. By tapping on a link a SecondActivity starts and the user can sign up. After the user completes the form an email is sent with a deep link to activate the account. When the user taps that link the MainActivity starts again indicating that the account was activated and that the user can log in. The problem is that in android 4.3 the previous activities get clear, but not in Android 5:
This is part of the code I'm using when the user taps the deep link:
Intent toLaunchMainActivityAgain = new Intent(this, MainActivity.class);
toLaunchMainActivityAgain.addFlags(toLaunchMainActivityAgain.FLAG_ACTIVITY_CLEAR_TOP);
toLaunchMainActivityAgain.setFlags(toLaunchMainActivityAgain.FLAG_ACTIVITY_MULTIPLE_TASK);
toLaunchMainActivityAgain.setFlags(toLaunchMainActivityAgain.FLAG_ACTIVITY_NEW_TASK);
When you are trying to launch another activity, call below to make sure Android Task Manager doesn't store the activity you are leaving
startActivity(new Intent(MainActivity.this, OtherActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK));
When I want to then go back to another activity from my current one, I simply call
finish();

Clear android stack and always start app on certain activity

I have an app that it password protected to stop unauthorised access to data.
What I need to ask is:
when app is closed using the home button, then reopened I need it to go to the LoginActivity, I think I have sorted this using android:launchMode="singleTask"
But if I press the back button it takes me back to the menu without the need for logging in again - so it bypasses the password?
Can you please help?
On your activity, before it goes to background by pressing home, set a flag that the user has logged out or clear the session.
Then onResume of your activity, detect if the flag for user is still logged in or session is still valid. If not valid. then just send an intent to open your login activity.
So even you press back, that activity that resumed will validate if the user is still logged in or the session is still valid.
Create a custom application class with a flag indicating whether the user is logged in.
public class MyApplication extends Application {
boolean loggedIn;
}
In your activity you can then check if the user is logged in. If they aren't, return them to the login activity.
protected void onCreate(Bundle savedInstanceState) {
MyApplication app = ((MyApplication) getApplicationContext());
if (!app.loggedIn) {
Intent intent = new Intent(this, LoginActivity.class);
//go to old activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}

implementing login like facebook

I have many activities in my application.
First is=Main.java
Second is=App.java
third is =Location.java
In main.java i have defined that if there is username and sessionid value stored in preferences then directly move to Location activity else show App.java.I have finished main.java in both cases.Main.java is just a splash screen.
I have defined an exit method in main.java that defines:
`android.os.Process.killProcess(android.os.Process.myPid())`
If someone logout from application then it comes on App.java.To close application on back button i have called Main.exit() which closes application in correct way.
And on location page i have used
`moveTaskToBack(true)`
which closes application.
But if i come back directly to Location page after some time my whole application does not work properly its session id expires.
And if i come from App.java page it works well.
I want to create it like facebook if u r logged in and closes the application it starts from second page.and if u logout then shows the login screen.
Please help me to resolve this.
Thanks.
I had something similar in my app. I wanted a way for the user to "sign out" of the application and not just the activity.
So, I did this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
System.exit(0);
That code launches the default home screen (exits the app) and then closes all of my activities. When the application is re-launched, it automatically goes through my MAIN launcher activity (which is login).

How to clear Activity Stack on Button Click in Android

I have a question that I have a logout button in my App on which we have called an App login Screen but at this point when user press the Back Button of Android Phone, he entered in the App again without Authentication, which is not desirable. I want when we click on Logout button All previous Activity Stack being cleared or we can say that All previous onPause Activities have to be cleared.
Please Suggest me the right solution for this problem.
Thanks in advance.
As far as I understood the login screen would be the first screen after the splash one so if login screen is in stack you can call again login screen like the below to achieve this
Intent launch = new Intent(context, LoginActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(launch);
After logout start login activity like this:
Intent launch = new Intent(context, LoginActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launch);
you need to use flag FLAG_ACTIVITY_NEW_TASK.
Alternative solution is to end your current activity by callingfinish(); after you start the login activity
// logout button handler
startActivity(new Intent(context, LoginActivity.class));
finish();

Categories

Resources