prevent to open another activity android - android

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

Related

Android - Start LoginActivity with no possibility to return back with back button

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...
}

BackButton takes me to home, but return to wrong activity

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.

Android : how to maintain session on back button?

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();
}
}

Stopping an activity and restarting it (android)

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.

Initiating activity for settings class

In my code I'm calling a preferences activity from my main activity which prompts the user to enter username/password, as below..
// Launch Preference activity
Intent k = new Intent(application.this, settings.class);
startActivity(k);
This calls settings.xml, but I want to re-initiate the main activity once the username/password is set and the back button is pressed.
Any pointers?
Do you want to reload main so you can use the username and password? If so, why not use startActivityForResult, validate the username/password in Settings.class and read the result in onActivityResult in your main activity?
I don't follow once the back button on the phone is pressed it should return to the activity that started the new one..
Unless you have a custom button in your app you can just use: finish(); in the onclicklistener

Categories

Resources