On start my app displays a splash screen and checks via network if the current user is still premium.
My problem: I started my app right before I went to bed and minimized it by pressing the home button. In the morning I launched the app again and it resumed the activity from the night. The app never really quit, my splash screen was not shown and and it couldn't check if the user is still premium.
So how can I achieve my app to be closed after a certain time (e.g. when the app is minimized)?
You should write the Premium user check logic in your onResume() method so that
if the activity is in pause or background state it will check the
logic every time it will be launched .
Don't try to finish app when it's minimized. Use Activity lifecycle callbacks.
#Override
protected void onResume (){
//check for changes here
}
If you want to end an activity, you call finish(). So you could record the time in onPause, then in onResume, check how long its been. If its been too long, call startActivity on your main activity, then call finish() to end the old one.
I think you should become more familiar with the Android Activity Lifecycle and think about which call back in your activity should you check if the user is premium
Related
I'm creating a music app in Android with a background service playing the music, and a Home activity that has various fragments and is the UI of the app.
When I press the home button on my phone, this app gets put in the background of course, and the lifecycle methods get called down to onStop(), in which the UI gets disconnected from the Service in order to allow background playback, the user can keep using their device and do other things. When I get back to my UI Activity, onStart gets called and the UI and Service reconnect together, giving me back controls over the music. onStart gets me the same activity in the foreground, it doesn't create another instance of the same activity.
I'm trying to implement also a function for when I press the back button on my device, so the UI activity can have the same behavior as with the home button, i.e. simply put the UI in the background (onStop). Instead, the default behavior of the back button is to finish(), killing the current activity it's called from (thus calling onDestroy).
What could I do for that? Couldn't really find anything online. Seems such a simple function that every music app has (not killing the app when pressing back, but just send it into background)
You can override onBackPressed and move the Activity Task to back. Based on the documentation Activity'r order in the Task will also remain unchanged:
#Override
public void onBackPressed() {
this.moveTaskToBack(true);
}
I want following behavior in my app:
1) Show notification on its launch: done
2) Do not show notification while switching through activities of the app
3) If another app is launched over this app, while coming back to the app again show notification.
I am planning to implement it via shared prefs(by storing a boolean value in it),
But cannot figure out the place to reset this value
i.e. how to know that app is exiting or switching to another app to reset this value(so it can show notification when app is launched again).
When an Activity goes to the background, onPause() is called, when it comes back to the foreground, onResume() is called. You can use that states to hide/show your notifications or even store your SharedPreferences.
Have a look at the Activity Lifecylce:
If you want to detect if all application's activities gone to background you need to extend Application class and register activity lifecycle listeners. Then just count calls to onPause() and onCreate() method.
http://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks)
Try to understand Android application life cycle.
Go through the following methods documentation.
onCreate() , onStart(), onPause() , onResume() , onDestroy() , onRestart()
Link
3) If another app is launched over this app, while coming back to the
app again show notification again.
To do this you need to overwrite back button and check activity stack. If it is 0 - show notification.
I am trying to mantain a log , when exits the applicaiton. I have used this code :
public void onDestroy() {
super.onDestroy();
Log.d("D", "Destroyed");
}
But this only works when I press the Back button. When I press Home button , the application Pauses , and If I close this application from task manager , then the onDestroy function is not called. How to handle this ?
Any idea ?
You can't handle the closing of application from task manager. In this case you're killing the app and onDestroy isn't called. You should make all clean up in onPause
You can do your stuff in onPause() method.
In your case:
If End Process is used from Process list in task manager, then nothing is called in application, the application is simply terminated.
If End Task is used from Applications list, then WM_CLOSE is sent to the window, which in turn allows application to do the cleanup.
onDestroy() is called when an activity finishes its life cycle. It is also called once in the lifecycle of an activity.
The OS decides when things "go away." The onDestroy is there to let your app have a final chance to clean things up before the activity does get destroyed.
From the Android Developer Guide:
There are a few scenarios in which your activity is destroyed due to
normal app behavior, such as when the user presses the Back button or
your activity signals its own destruction by calling finish(). The
system may also destroy your activity if it's currently stopped and
hasn't been used in a long time or the foreground activity requires
more resources so the system must shut down background processes to
recover memory.
When you switch between apps by pressing the home button, Android pauses the activity and resumes it when you return to the activity.
For the most part, the OS decides when to quit an application so it wouldn't make sense for you to log when an activity is destroyed. I would suggest overriding the onPause() or the onStop() method
I have a problem with my application when it is brought back to the foreground in the case the phone went low on memory while the application was hidden:
The class inheriting from Application is re-created (onCreate is called again), thus losing data it held before. The Activity which is restarted is not the one tagged as main action in the Manifest, but the last one that was active. This is a problem as the main activity, from which the user logs in, is the one responsible for filling in the Application subclass' data and I can't fill it in later.
Is there any way to tell the application to restart at the main activity instead of the latest one in this case ?
You could check in onResume() if the user is logged in. Means that you check if your Application data is filled. If this is not the case, finish the activity and start your first Application.
The user expect to return to the latest activity, so a general "always start first activity" would upset the user...
you could keep the data by implementing methods such as onPause() and onResume()
Check this link out: http://developer.android.com/reference/android/app/Activity.html
this answer might also be helpful: onSaveInstanceState () and onRestoreInstanceState ()
I am trying to detect if my app became active from multi-tasking and display a dialog.
The use-case is something like this: User is using the app -> Hits the home button -> Does something -> User taps on the app again
As expected, the last activity the user was on is being shown.
I want to display a dialog when this happens. I used onRestart() for the same and it works. Only problem is, even when the user goes to some activity in the app and then hits back, the dialog is being displayed.
I went through the activity lifecycle several times, but couldn't figure out the solution. Any ideas?
Well, you can tell the foreground application using the technique outlined in Determining the current foreground application from a background task or service: look for getForegroundApp. You could run a timer that checks periodically to see if the app is foreground, and if its not then set a variable after a suitable delay (to make sure you don't happen to hit it in the wrong order when switching Activities). Clear the variable in onStart, and if onCreate of the rooth Activity is ever called you know that the app just became Active.
I achieved this by setting a flag in Shared Preferences in onStop() and cleared it in onDestroy().
Then I overrided the Back button to clear the flag whenever it is pressed. This solves the problem I had stated.
Now in onRestart(), if the flag is true.... I display the dialog.
I know it is not the most elegant solution but does the job! Hope this helps somebody.