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.
Related
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
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'm trying to trigger an action such as showing a popUp on the Home activity of my application when the application is launched or if it was running in the back ground and was brought to front again.
My problem is that there is no onResume() or onRestart() for the Application class, and at the home activity's side there no way of telling if the previous activity was not coming from the my application or not to decide on showing the popup or not.
is there a listener that can be trigged on return to the application?
Thanks in advance.
You should actually put it in the onCreate() method, and do a test for what intent it has been passed. That way you can distinguish when it's been called from another of your activities.
Unfortunately, it's not possible to read the task stack, or determine if the user came from the home screen, see: Android - detecting application launch from home or history
This might not work for everyone, but why not create a base class for the activities in your project? Then you would have access to onResume() and onRestart().
If you are extending other SDK Activities (ListActivity, etc), you could create base flavors of those classes and move the code called in onResume() and onRestart() to a utility class so that the same logic can be called from any "base" Activity.
Here is the scene what i am facing..when i press home key it saves the current state of the current activity..so when my app comes in foreground, onResume() of that particular activity gets called who's state was saved. So if an app with say 10 activities are there then, we will need to write reload of app data in each activity's onResume()..is there a way to specify a reload of app data in one activity only..?
Jitesh,
Yes, this is possible. You will probably want to perform the reloading of application data in your Activity's onResume() function. See Android Activity Lifecycle.
I have this requirement to send my application background and then bring it to foreground on some key capture intents (not from application launcher offcourse) So How can I send the current tasks to background and bring the same to foreground ?
Use moveTaskToBack() to send the activity in the background and still running if the user presses the back key.
see :Activity for the way on how to do this. its quite simple.
so in order to do this you will also need to override the onBackPressed() method or onKeyPressed() and call this method if the back button was pressed (dont forget to return true on the back pressed methods so android is aware that you consumed the event and doesnt finish the activity).
For returning to this activity that you have moved to the background you can post a notification with a pending intent to launch it back and that will automatically bring the activity to foreground.
Hope this helps.
To send you application to background you should call moveTaskToBack() from your Activity class. When your Activity gets new intent (btw. the onNewIntent() method from your Activity will be called) your Activity gets into foreground by system (you don't have to do anything).
What do you mean by "background?" Activities are stacked one upon another as you create new Activities, then accessed in reverse order using the device's back button. Think of the push() and pop() methods, it's the same paradigm. Applications that need to have code running non-interactively should extend android.app.Service, but beware that you can do some real damage implementing a service. Rogue processes can drain battery life and reduce UI responsiveness.
I solve all the problem pertaining to notification start with fresh activity after moveTaskToBack(true) when back key is pressed by
adding to manifest android:launchMode="SingleTask" android:clearTaskOnLaunch="true"in the activity xml markup section