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);
}
}
Related
I wrote a messaging application which requires an XMPP connection. Everything is ok when user is changing pages inside the application. Everything is also ok when I send a notification to him and he clicks it to come back to my application. But when he tries to come back to my application from his "last applications" page my application redirects him to my login screen. And since he is connected he cannot login again, hence he is stuck in there. How can I redirect someone from his main menu to another screen other than login(I think it sends him to login because its my main activity.) With sending notifications I was easily able to specify the intent target. Here is what I tried in loginActivity.
protected void onRestart() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean loginStatus=prefs.getBoolean("xmpp_logged_in",true);
if (loginStatus) {
Intent intent = new Intent(this, ConversationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
super.onRestart();
}
I have a solution but it is not a pretty one. My app follows this screens Login-Show Contacts- Message with a user.
So in Show Contacts I overwrited onBackPressed and made it same with home button. Here is the code
public void onBackPressed() {
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
}
I would recommend to put your code inside onResume() instead of onRestart() since it is more reassuring that the lines of codes here are called every time your app gets into foreground state.
Your code seems okay. Can you make sure that you called apply() or commit() to your Shared Preference transaction after successful login?
For instance,
prefs.edit().putBoolean("xmpp_logged_in", true).commit();
I am setting up an App Widget, When the user adds the widget to their home screen, It pops up with my configuration activity.
but now I need my user to login, SO i redirect them to my login activty,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isLoggedIn()) {
Intent intent = new Intent(AppWidgetConfigureActivity.this, LoginActivity.class);
intent.putExtra("Source", "widgetConfig");
startActivity(intent);
this.finnish();
}
But now when my login activity has successfully logged in, I need to create my appwidget config intent and start it up again.
But im not sure how to do this, because the config activity contains information like the widget id etc.
How can I start-up my config activity again?
The answer may be as simple as not calling finish(). This will keep you configuration activity on the task stack and when the login activity finishes, the configuration activity will be displayed again.
If you used startActivityForResult, you could use your login activity to get some data that can be used when coming back to your own activity.
I'm implementing an application locker for android.
I have the following code in my onPause() of authentication activity where user has to enter his password.
#Override
protected void onPause() {
super.onPause();
blnSwitchingActivity = true;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)
.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
The following scenario creates a problem.
The user first clicks on any app.
The authentication activity is opened. The user can either enter his password or go back.
If he goes back, in onPause i'm calling home screen intent.
The problem is when the user clicks on home screen, he has to wait for a few seconds to open any other app.
My question: Why there is a delay in calling the home screen intent?
I'm not 100% sure of what your question is, but it sounds like some combination of android:noHistory=true and android:excludeFromRecents=true for the Activity in your manifest would do the trick for you.
I have a registration form in android wherein the user can enter data from it. The user can search for Clinic. After that searching, the user submitted the record, and logout. If the user doesn't have no business with that app, the user can exit BUT when the user pressed the Exit button, it does not exiting, instead it goes to the Searching activity all over again even if the user is logout already. Can someone help me figure out why I can't finish my activity?
Search Activity
btn_Close.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Intent myIntent = new Intent(getApplicationContext(), RegForm.class);
myIntent.putExtra("from", "Search_Cancel");
startActivity(myIntent);
Search.this.finish();
}
});
Android Manifest
<activity
android:name=".Search"
android:noHistory="true" >
</activity>
Activities are finished with the this.finishActivity(Activity.RESULT_OK); call.
Note: You may also specify other results located in the Activity class as static finals.
finish() will make your active Activity pop from the Back stack but your others Activities will remain on that so if you want to exit whole app you should use Flag FLAG_ACTIVITY_CLEAR_TOP when you are starting your Last Activity to Clear your Back Stack
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