My App contains two Activity Classes. from first activity user can login and from second activity user can view their project details.I'm maintaining session also with logout button. In my app if user clicked on back button from second activity then it goes to apps section(means main menu) and after some time user clicked on my app icon it goes to first activity not second activity yet user didn't logout. I want user should go to second activity not first activity if user already login. I had tried but
not working and still I am trying.
How can i do the above stuff please anybody suggest me the answer.
You should save the state of current user in to SharedPreferences.
In the onCreate method of your logIn Activity, before you call setContentView(), you can check if the user is logged in. If this is true open new Activity wish startActivity() and call finish() on current Activity. If not just continue with the normal setContentView() call.
This way the logIn activity will not be visible if she state inside SharedPreferences is set to logged in.
You do not have to display first activity. But in fact there is always main activity. That should decide whether to start directly login activity, or to display user content directly.
You can use finish() from main activity as soon as you started login or content activity, so when user pushes back(), he will not see main activity again.
MainActivity extends Activity
{
public void onCreate()
{
if isLogged()
startActivity(new Intent(ContentActivity ...));
else
startActivity(new Intent(LoginActivity...));
finish()
}
LoginActivity extends Activity
{
...
public void onLoginSuccess(String username)
{
// called from dialog OK button and login process success
startActivity(new Intent(ContentActivity ...));
finish();
}
}
Related
I have an activity that handles users login.
When user logs in he is forwarded to another activity. But when he presses "back" button he is back to login screen despite he is logged already. How can I avoid that behaviour?
Clear activity stack after user is logged in or use noHistory flag. Start login activity with intent that has these flags : http://developer.android.com/reference/android/content/Intent.html
Here is one way to do it
Intent afterLogin = new Intent(this, AfterLoginActivity.class);
login.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(afterLogin);
You just add this function in your second activity it will give your desired...All the best it will work 100% All the best
#Override
public void onBackPressed() {
// You should write the super function or finish() here...
}
I have an android app with a loginActivity and then MainActivity.
The startpoint is the loginActivity, when you first open the app you log in and then it starts the MainActivity.
I overrided onBackPressed from the MainActivity so you wont get back to loginActivity, unless you tap a logout button.
#Override
public void onBackPressed() {
Intent mainActivity = new Intent(Intent.ACTION_MAIN);
mainActivity.addCategory(Intent.CATEGORY_HOME);
mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mainActivity);
finish();
}
The problem is, when i press back button, it shows the home luncher (as expected) but when i open the app again, it goes to loginActivity.
Is there any way to "bypass" the loginActivity or to tell android in which activity i stay last time?
I would set a preference if the user already logged in. In the LoginActivity's onCreate you can check the preference and start HomeActivity if needed + call finish() in LoginActivity. This way you don't even have to override onBackPressed later and everything will just work.
I would suggest changing the entry point of your application to the MainActivity, doing a check within that activity to see if the user is logged in, and redirect to the login activity if they are not logged in.
My app begins with Activity A (log in screen) which I always want on the bottom of the Activity stack.
Based on some logic after login, I launch some other Activity B-Z. There is a menu in the app which allows you to switch around between any Activity B-Z.
If the user hits the back button enough times, I don't want them returned to the login screen. I want the user to be sent to the Android Home screen if the back button is pressed on the Activity which has Activity A as the next Activity on the stack. This Activity is not guaranteed to be the Activity which was launched by Activity A because my Activities use singleTop.
Ideas?
The only other option I can think of is to remove singleTop and whatever Activity is launched by Activity A could remember that (my Activities all derive from a base class, which I would use to do that).
Another possibility may be to do something like the following in the onBackPressed handler:
if (getParent().getClass().getName().equals(ActivityA.class.getName())) {}
Although not a direct answer to your question, but if the problem is that
I don't want them returned to the login screen
then the classic solution is to finish() the login Activity when the user has logged in successfully. This way you'll make sure the user will never return to that Activity.
To do this, Why don't you get the User Login information, and store it in your apps private shared preferences. Then when the User launches your application, you can read the Shared preferences from activity A, and automagically log them in to activity b .
something like this:
SharedPreferences myPrefs = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);
if((myPrefs.getString("username",null) != null) && (myPrefs.getString("password",null) !=null)){
// Make sure your user is a member of your application
// auto log in to the home page
Intent bIntent = new Intent(getBaseContext(), BIntent.class);
startActivity(bIntent);
}
For more reference on how to use Shared Preferences
http://developer.android.com/reference/android/content/SharedPreferences.html
Then if you want to be really slick, Store the Class name when the onDestroy() method is called in each Activity, overwriting each class as the last open Activity. So whichever activity the user was last on before the application was closed is stored in preferences and you can read that in the login activity, then from there launch b-z
This will make it so that the login activity is always in memory, since it is first launched to check your users credentials.
I have 2 activity which are : main activity and login activity.
when login is successfull in login activity then intent will open main activity.
The problem is everytime i press back button the login activity is opened again. i want to minimize app instead of opening login activity again.
i use shared preferences to flag my app if it is loging in. i put boolean value true if the user is logged in.
I used shared preference. and use cekLogin() like this :
private boolean cekLogin() {
boolean login;
login = config.getBoolean("login", false);
return login;
}
config is my sharedpreferences.
if(cekLogin){
//do not open login activity again when back button pressed <--- i don't know how to do this
}
Put this code in your login.java file
//Assuming this is the part where you open the second activity:
Intent intent = new Intent(this, secondActivity.java);
startActivity(intent);
finish();
When you click the back button on the second activity, the application will go back to the home page since you finished the login activity.
In your AndroidManifest.xml add this inside the LoginActivity tag:
android:noHistory="true"
This will switch off the history for this activity so that when you press back button, this activity will be always skipped. Since you get to the MainActivity only if Login is successful I think this is OK.
You can override onBackPressed() so that when the user pressed back the app closes, instead of returning to the previous activity.
Or you could finish() the previous activity after starting an activity on top of it.
The problem is everytime i press back button the login activity is opened again.
for this you got two options: either you finish the login activity and then go to main activity, or you may override the method onBackPressed()
When login is succesfull store the value in sharedpref before that you have to first check whether their is login already in sharedpref if yes then it will directly open the main activity otherwise it open the login screen.
if(isLogin)
{
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
}
else
{
startActivity(new Intent(SplashScreenActivity.this, LoginActivity.class));
}
where isLogin is a boolean variable declared false first and change the value to true in sharedpref if it has been loginsuccessfull
I have a login screen on my app, and as of now you can press the back button to go back to the login screen. I do not want that to happen.
Once the user logs in, the app should stop the login activity so the user can't go back to it. On the menu screen I have a logout button that I want to use to restart the login activity after already being stopped.
I have been looking and testing for an hour or so and I can't find the answer.
Set noHistory = true in the manifest for your login activity. This prevents the activity from being stored on the back stack.
If you do not want user to be able to get back to login activity, simply exclude it from recent stack, by adding to your activity declaration in application manifest android:excludeFromRecents attribure, like this:
<activity android:name=".Login" android:excludeFromRecents="true"></activity>
then once your login activity finishes it task, you (most likely) go to next activity of your app. And once you call startActivity() to lauch new one, simply call finish(); to finish login actvity:
...
startActivity( intentToLaunchNextActivity );
finish();
On successful login you can call Actvity.finish().
To restart the login activity use Activity.startActivity(intent).
Try either:
After login succeeds in your login activity, call finish() on the activity after launching the next activity. This will effectively destroy the login activity and remove it from the activity back stack (so it can't be navigated to anymore). Something like:
onLoginComplete(){
startActivity(new Intent(this, NextActivity.class));
finish();
}
Or, in the onResume of your login activity check the login status and finish if true. This way, whenever the user comes to this activity it will be closed (and they will never actually see it) if they are already logged in. Something like:
#Override
public void onResume(){
super.onResume();
if(isLoggedIn())
finish();
}
When you leave the login screen on a successfull login, call finish() this will remove your actvity from the stack.
Your logout button should re-call the login page like this:
Intent intent = new Intent(TheCurrentActivity.this, YourLoginScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
This will load up the login screen and clear the current activity stack ensuring that you can't go back after logout.