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.
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 have one problem regarding my app,when i click back button my application is getting finish.i need to prevent it.How could i make it .
#Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
startActivity(setIntent);
}
I have tried something like this.But its not working .Actually what i need is when i will click back button ,the application should not finish it should run in background.can anybody help me out.#Thanks
Try doing it like this:
#Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
By doing this, your activity will not be destroyed (i.e. onDestroy will not be raised). But also, there's no guarantee that Android will preserve your activity for long.
In case, you are running a process that you want to keep on running even in background, then I would suggest you to go for Service or IntentService.
What do you mean by "it should run in background"? Why would you want that? If the user wants to keep the app opened, he can use the home button and the app won't be closed, if he presses the back button he wants to close the app. If want to have something that is still running even after the user closes the application you should take a look at Service http://developer.android.com/reference/android/app/Service.html, this will continue running even after the user closes the app
I want some directions here. I have 3-Activities in an app i.e. MainActivity, MapViewActivity and DbActivity. I goes from one activity to another via. intents. The code of each intent is:
Intent intent = new Intent(this, ActivityName.class);
startActivity(intent);
I have used
android:noHistory="true"
in all these activities, so that when the mobile "end" button is pressed the app gets stop. Otherwise app was going back..... in spite of stoping the app
Now the problem is: When app goes for the GPS settings after finding GPS service off, the user turns on GPS and presses the mobile "end" button to come back to the app. But app also get stop. So user again start the app and comes on MapViewActivity. If I remove android:noHistory="true" from MapViewActivity, the problem also comes back. So is there any other solution for. Please guide me, if there is some method...
I don't think you mean "end". You probably mean "back". If you don't want the user to return to the activity when the user clicks "back", then just call finish() after you start the next activity. Like this:
Intent intent = new Intent(this, ActivityName.class);
startActivity(intent);
// finish this activity so that the user doesn't come back to it
finish();
Please take out the android:noHistory="true" from the manifest, this probably isn't what you want.
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.