my Main launcher activity (the one that has android.intent.action.MAIN as its action) is Login page.
After successful login I started HOME activity and finish() the LOGIN one to prevent users returning to that page by pressing BACK button.
When I press SIGN OUT button, I want the app to return to Login page. But I can't find a way to do it. Here's the sign out code:
//This method is in HOME activity
private void signOut(){
Intent i = new Intent("android.intent.action.MAIN");
startActivity(i);
finish();
}
That code will open dialog box listing all applications in my phone for me to choose. I tried putting the package name + class name (com.example.test.Login) as the Intent but keep getting this error:
android.content.ActivityNotFoundException: No Activity found to handle Intent
I know I can trick this problem by using Splash screen as Main activity. But If there is another better solution, I want to know it.
Thanks
Use
Intent i = new Intent(this, LoginPage.class);
startActivity (i);
finish();
Related
I've been trying the new Navigation component and so far so good. However, I've hit a wall when it comes to the following. My question is best described with an example, so let me give one.
I think it's a very common scenario that apps have a login screen and then they forward the user to a home screen, or dashboard, or something similar. Let's stick to the naming - LoginScreen and HomeScreen.
Say the app is very simple. It starts at the LoginScreen and as soon as the user logs in, they're forwarded to the HoneScreen. Pretty simple app, but serves the example.
Now, if the user hits back, how do we exit the app? So far it always takes me to the LoginScreen.
Further evolving on this example. Imagine there's a welcome screen before the login screen where the user can decide to login or register. How would one deal with navigating back in this case too?
Essentially, I'm asking if there's a built in functionality in the navigation component that let's you go back more than one screen and if not, is there a way to achieve this? Thank you very much.
#Fred
To achieve this you can finish each activity after having started the other one. For example the WelcomeScreen has called LoginScreen you can directly finish it and when LoginScreen calls HomeScreen finish LoginScreen so when the user will navigate back from HomeScreen all activities will be closed.
Or, from the present activity you can call System.exit(0) to exit the application. This serves when you have many activities but if you have one there will be no difference with finish()
Another scenario is as you described in comments:
Consider you have following activities: ActivityA, ActivityB and ActivityC and you want to come back to ActivityA from ActivityC without passing by ActivityB. If you ActivityA has been previously opened you can bring it back to front, depending on when you want this to be done. With our sample we consider when the activity finishes:
#Override
protected void onDestroy() {
super.onDestroy();
//this will bring the ActivityA on the front
Intent intent=new Intent(this,ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
Or you may have a different scenario where the LoginActivity is started only if the is not already connected otherwise you do directly to HomeScreen. In this case, I create a Singleton that will help me register the current calling activity so that I can know which activity will be directly started after successful login.
For example:
if(sharedPreferences!=null) {
if (sharedPreferences.getBoolean("user_connected",false)){
startActivity(new Intent(this, HomeScreen.class));}
else
{
MySingleton.getInstance().setCurrentCallingActivity("HomeScreen");
startActivity(new Intent(this, LoginActivity.class));
}
}
And once in LoginActivity, after a successful login, I do the following:
try
{
startActivity(new Intent(this,
getClassLoader().loadClass(MySingleton.getInstance().getCurrentCallingActivity())));
finish();
}catch (ClassNotFoundException e)
{
//your code
finish();
}
That's how I handle that and it works. Up to you to see a corresponding scenario and try if it can work. Different approches may coexist and this far from being performant.
You could try this:
Intent myIntent = new Intent(LoginScreen.this, HomeScreen.class);
startActivity(myIntent);
finish(); //finish LoginScreen, then when press back in HomeScreen, it will exit instead return LoginScreen
First of all you need to see if u prefer to use Activity or Fragments.
if you want to remove all Fragments in the stack then you need to use
FragmentManager fms = getSupportFragmentManager();
for (int i = 0; i < fms.getBackStackEntryCount(); ++i) {
fms.popBackStack(); }
If you want to remove specifc Fragment than you will have to use tags.
Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
Now about Activity you need to consider a few options
Activity A is your Login.
Activity B is your Register page.
Activity C is your Homepage.
if you want to move from A to C after Successful login than use
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
if you want to move from A to B
Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);
if you want to move from A to B and after Register success move to C than use :
Intent intent = new Intent(LoginSM.this, MainActivitySM.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
I'm new in android development, and there is something about the life cycle activity that I don't understand, especially with the following example of application that i'm working on.
In my app, I have a Login activity and a Main activity.
In my Login activity, with successful attempt, there is a Intent that start the main activity, and finish() the login activity.
==> There, my login activity is destroyed, so this should not show up again.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("authentResult", tokenDto);
startActivity(intent);
finish(); //destroy activity to not open it with back button`
In my Main activity, I have a disconnect button that create an Intent that start a (new ?) login activity.
==> Until there, everything's normal, and the login activity is displyed.
Intent loginActivity = new Intent(this, LoginActivity.class);
startActivity(loginActivity);
In the login activity, using the Back button should close the app.
To do that, I send an intent with special flag to the main activity to finish it (So the back button will not wake up the main activity), and then I finish the login activity. The onDestroy method is called and I see the login window close itself.
==> From here I expect the app to be closed. But a "new" login activity shows up, and i suspect that it would be the activity of the first point, so I'm a little lost there...
public void onBackPressed() {
Log.d(TAG, "BACK PRESSED - loginActivity");
//Finish MainActivity
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
finish(); // finish login activity
}
In the onCreate in the mainActivity, I begin with this :
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Do anyone could explain to me what I'm missing, or show me a better way to close the app directly ?
Don't hesitate to telle me if something's not clear.
If you declare the Login activity as main activity in the Manifest, if you don't destroy it when you launch the second activity then i think the back button will do all you expect without any additional code, and if you press back key on the login activity it will go to phone home screen
On Android applications is the system that decides when to close/kill application.
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
I have LoginActivity which checks SharedPreferences for login details then it redirects to HomeActivity and other activities after that. I have put Menu item Sign Out on each of this activities and used this code on sign out button.
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
How to hide the Login activity, so that when user press back button from home screen it'll close the app.
Like when I open app it shows Home screen and when I press back button normally it should close the app. But in my case it takes me to the Login screen which is the first screen checking user credentials.
I cannot end the Login activity, otherwise that solution doesn't work.
I'm a new to Android. Please suggest something to solve this problem.
you should try this on your home Activity's back key function:
onBackpress(){
Intent intent = new Intent(mContext, LoginActivity.class);
intent.putExtra("FLAG", 0);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
and on your LoginActivity just do:
onNewIntent(Intent intent){
int i = intent.getIntExtra("FLAG", 0);
if(i == 0)
finish();
}
remember launchMode for activity in menifest should be singleTop.
Override the Activity.onBackPressed() method and then send the application home via an Intent.
From the SDK:
An intent with the following categories will allow you to go home.
ACTION_MAIN with category CATEGORY_HOME -- Launch the home screen.
You can override the back button keypress event and have it close the app when pressed.
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();