I want a notification to pop up when an app opens. But once User dismisses, I don't want it to come back again, even if they go back to the same activity. But when the app exits, and they come back later, I want same dialog notification to pop up (prompting user to login).
So basically...
boolean b = true;
if (b == true) {
// show dialog
b = false;
}
I simply want var b to save state but clear on exit.
I'm not sure there's a point to using sharedpreference if you don't want the value to persist across your app being closed, as that's kind of the point of them.
Perhaps just using a boolean in your Application class would be better? It will be persistent until the app is completely shutdown.
android has no concept of "app exit", only the android lifecycle, so you'll have to be more specific about the behavior you want. for example, do you mean whenever the user causes any activity in your app to be paused and no activity in your app to be resumed?
if you only have one activity, then you can just keep a state variable in the activity setting it to false when paused and true when resumed.
if you have >1 activity, keeping track of when none of your activities are in the foreground any longer is painful. for example, if you set foreground=false whenever an activity pauses, and foreground=true whenever it's resumed, and then check a time tolerance. e.g., if the time between pause and resume is greater than some threshold, assume you are coming back from the background.
You don't want to use SharedPrefs unless they logout out of app with a button or some other listener, you can't be sure to change it. You can put it in onDestroy() to reset the variable or onPause() even better if you want to make sure it gets reset
Related
So I have a SettingsActivity that will launch my SettingsFragment when the user clicks on the settings button in MainActivity. For my SettingsFragment, when the user launches the screen for the first time, it will activate the AlarmManager I have set it up. This defaults to on when the user signs in for the first time. The user does have the option to turn off notifications.
Now, if the user never goes to the settings screen, it will never activate the AlarmManager. How do I start the SettingsActivity in the background where it doesn't actually show the UI of the SettingsActivity or SettingsFragment and then close the SettingsActivity/SettingsFragment immediately so it's not running in the background forever? I just want to start it up when the user logs in for the first time or logs back in again so the app knows the state of the AlarmManager whether it's on or off. If it doesn't know the state, the user will never get any notifications until the user manually clicks on the settings button.
First, understand the activity lifecycle: https://developer.android.com/guide/components/activities/activity-lifecycle
You can finish your activity at any time (for example onResume()) by calling finish(). You can also use a Timer, a Handler or an Alarm to finish() it at a different time.
In order to make your activity transparent / invisible, don't inflate it with any kind of layout. I would also recommend adding the following lines to the manifest for your activity:
android:launchMode="singleInstance"
android:excludeFromRecents="true"
Regarding the ability to start this activity without any kind of user interaction... you may want to use a Service and startActivity() from there (and potentially also finish() it).
That said, from what you are describing I think you are designing this in a very complicated way and may run yourself down a rabbit hole.
You should let your SettingActivity be your SettingActivity.
When you not use SQLITE or something else to store settings: https://developer.android.com/training/data-storage/shared-preferences
Putting all your Code into the SettingFragment should not be your Solution. You could something like this do:
return this.sharedPreferences.getBoolean(context.getResources().getString(R.string.preference_stateAlarmmanager), false); // returns false if the preference not yet exists
Just read your property and execute your code to start the alarmmanager in case it's true
I'd like to do certain, simple action every time that my app is closing - I just want to decrease int variable. How to do that?
As #Ozair has rightly asked that it depends what do you define as closing of app.
You can do either or both of the following depending on the need:
If you want to detect closing of app by use of BACK button, then from within your last activity, you can detect the pressing of BACK button by overriding onBackPressed function. There you can decrement your value.
If you are also considering the situation when you app goes into the background by pressing of HOME button, then in your activities you would have to detect the HOME button pressed. There have been many solutions which no more work for detecting HOME button but this answer on How can I detect user pressing HOME key in my activity? question seems to work for me.
So, there you can detect the HOME button and decrement the value which you can save in SharedPreferece.
There can be other cases where you are calling finish() and closing your last activity. It is not clear from your question if you are considering that case as well.
Hope this gives you some opportunity to think about it.
The question is what you mean "close"?
If you close all your Activities, the App-process might still be running. If you mean that the "close" is just closing all of your Activities. You might define a "count" for all opening Activities, you can store it in DB or SharePerference. I think you can do follow(dummy codes):
In your project, you should define BasicActivity:
public class BasicActivity extends Activity {
onCreate() {
mPreference.incActivityCount();//++
super.onCreate();
}
onDestory() {
mPreference.decActivityCount();//--
if( mPreference.getActivity() == 0 ) {
//All being opened Activities have been closed.
onAppHasNoUIs();
}
super.onDestory();
}
onAppHasNoUIs() {
//All being opened Activities have been closed.
}
}
I'm working on an Android app that will show college fitness professors how their students are doing in their classes. Since this data is fairly sensitive (biometrics are shown, including weight, something many college students are self-conscious about) I don't want the data to be available to anyone who picks up the tablet. While I have a proper login screen created, complete with authentication for the database, etc. I have an issue when the home button is pressed. Since Android doesn't close a program immediately on leaving the app, it's possible to reopen it and return to where you were. I would like to force the app to return to the login screen each time (I've altered onBackPressed so you can't just return to the previous view from the login screen) so that you have to re-enter your credentials to get back into the app. However, I can't seem to do this. An answer I found on here said to use the following line:
android:clearTaskOnLaunch="true"
However, no matter what XML file I put it in, be it the Manifest or the individual Activity XMLs, it appears to do nothing. So, how do I ensure the login screen comes up each time the app is launched, regardless of whether it is starting from scratch or not?
Try to play around with onUserLeaveHint() method. If you read its documentation, it says:
Its Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called
So, when ever you detected home button pressed, you can finish the running activity/activities. So next time user click the app, it will start from the first login screen.
Hope this helps.
You should override onUserLeaveHint()
#Override
protected void onUserLeaveHint() {
// do your logic
//finish() or start login activity
}
You could set a flag when onPause() is initiated within the activity. And then when you return you could check the flag from within onResume() and then request a login from that point. This will be sure to request it each time; in a simple case of course.
Edit:
With multiple activities, you could check against a saved context to see if they are the same when you start a new activity. If the contexts differ then you can discard the context previous activities context and start a new activity.
If they are the same, then you have come back to the activity from itself (you have lowered and brought the screen back). You would have to use some form of saved state such as that to do it in this manner with multiple activities when outside the case of a simple application.
I found out how to do it in my case. For any others with the same problem, try following the example here:
Android detecting if an application entered the background
My Android application(Application A) launches another application (say Application B) upon click of a button.
Now I want to implement "auto start" functionality wherein Application B will be launched as soon as Application A is launched.
For this, I created a checkbox and used SharedPreferences to store the value of checkbox.
Then, in my onCreate() method of Activity A, I am checking the value of checkbox from SharedPreferences and launching Application B in case the value is "true".
The Problem:
The problem I am facing is, when the user exits "Application B" (and comes back to Application A) the onCreate() of Application A is getting called again and Application B opens up again. This sets off a infinite loop and at exit of Application B, user returns to Application A and goes to Application B again.
I know onCreate() gets called multiple times (when we change orientation, keyboard opened, Activity goes into background and is killed by system), but is there any clean way of doing this?
To reiterate, my requirement is to launch Application B from Application A if the "auto start" checkbox is checked in Application A.
My suggestion will be to use the method onPause of the activity in application A and set a flag there "application B was called". Then if this flag is set do not call application B in onCreate of the activity in Application A and unset the flag.
If application B is too long in the foreground application A might be suspended by the system and the flag will be reset. In such case maybe it is good idea to have the flag stored in some persistent storage (e.g. SharedPreferences).
EDIT One more thing: the flag should be set in onPause only if the activity is being paused, because the other application will be shown (this will be easily determinable, because everything happens in the same class).
I have to know when my app comes back to the foreground in these situations:
Start app
Start Activity A
Home Pressed
App brought to foreground by user. (Activity A is shown)
Start app
Start Activity A
Start Activity B
Home Pressed
App brought to foreground by user. (Activity B is shown)
I cannot check this with a flag in onResume and onPause. Because this would trigger every time Activity B is loaded.
I'm not sure if this would work but try overriding the onUserLeaveHint of your Activity:
https://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
If this one is called, set a boolean field of your Activity to true. When the onResume is called, check for this boolean. If it is true, then your Activity is coming back into the foreground (don't forget to set this boolean back to false again).
BTW: I like your KlaasVaak handle :). Are your trying to make people sleepy :)