I need to update user information everytime the user after working with the Android App closes the App.How can do the same. do i have some onDestroy kind of method in Application that i can use for the same.
Kindly update.
thanks
There absolutely is such a method - and you guessed it's name correctly. You could override the onDestroy() method.
However, onDestroy() is also called with the application is rotated. There are other issues as well.
Android doesn't really have a concept of closing the app. The best way to do this is to save onPause() - which will be basically every time the user leaves the app for some reason, such as for multitasking. Just override the onPause() method and go from there:
#Override
protected void onPause() {
super.onPause();
saveData()
}
private void saveData() {
//do whatever to save data here
}
I would also read about the android application lifecycle to find out more about how all of this works.
Related
I want to perform logic whenever my Android app is in the process of being closed, so to avoid duplicating the same logic on each Activity.
Similar to OnCreate() below, which is invoked whenever the app is starting, I need the close/terminate method.
public class Application : Android.App.Application
{
public override void OnCreate()
{
}
}
Most of the times your Application class will not know that it is going to shut down because usually it is just killed by the system. You can hide activity but you can't hide application since it is always there (as long as process runs). And if user (or system) decides to manually kill it you will not get any chance to save your data since it might be time consuming. So the best way would be to call custom method in Application from your Activity's onStop() and save everything you need. And it will be a good idea to save data in background thread.
In activity class:
#Override
protected void onStop()
{
((CustomApplication)getApplication()).onCloseCustom();
super.onStop();
}
In your application class
public void onCloseCustom()
{
//do whatever you need
}
I want to make a exam app I want to apply a feature in my app in this feature when student minimize app then exam will automatically cancelled. so they can't cheat so please tell me what will and how will I do this in Android app in adt.
In your exam activity, override onPause() and paste cancel exam method before super.onPause().
Edit: I think you need onPause() instead of onStop(). Learn more about Activity Life cycle
When you minimize app, onStop will called.
Called when you are no longer visible to the user
So inside onStop you can cancel the example
#Override
protected void onStop() {
super.onStop();
// cancel exam here
}
I'm try to implement a call log in my Android app, which reads the system call log and shows on my app. There's a button to make a call to someone in my app with the code of:
startActivity(new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+dataList.get(arg2).get("number"))));
When I press the button, my app will lose focus of the screen and it will jump to the calling page. When the call finishes, I want the call log in my app could be refreshed, I mean, the call just now should be added to my list. So I add refreshing functions in the onResume() of my Activity.
But it weirdly turned out the list didn't refresh when the call finishes(the screen automatically turns back to my previous page of my app, say the list).
So here comes my question, shouldn't be the onResume() function be called when the call finishes? Meanwhile, the onPause() wasn't called either, when jumping to the calling page. I'm pretty sure they weren't called cuz I add system.out.println() at the beginning of onResume() and onPause(), and I saw nothing.
Could anyone here help me with this problem? Thanks for all your read and help.
Make sure you correctly override the onPause() and onResume() methods. (no parameters, void return type)
You will also need to call through to the super method in each case, too.
That is:
#Override
public void onPause()
{
super.onPause();
// your code here
}
#Override
public void onResume()
{
super.onResume();
// your code here
}
I'm working on my 1st Android app and wondering how to handle activation/deactivation/starting/stopping globally, not on Activity level.
This great article shows states transition for Activities:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Is there something similar for Application states?
For example at iOS and Windows Phone app there is clear app states separated from activities (views, controllers, whatever).
I'm asking because I want to perform certain operations only once per app loading/exiting not with every activity starting/stopping
The answer is There is Simply No Direct method to do this
rather than in Application Class you can catch these events
#Override
public void onLowMemory()
{
super.onLowMemory();
}
#Override
public void onTerminate()
{
super.onTerminate();
}
So you will have to handle it in all the Activities you will be having
the following methods
onResume()
onStart()
onRestart()
onPause()
onDestroy()
You will have to implement in all Activity to handle for all application
A suggesstion
You can have some Variable in Application class to save application state
say create a variable like
public static boolean isPaused;
and set it from all activity on state change
The question you're asking is applicable for iOS and Windows but not really for Android.
Android doesn't really have a concept of an application as an object, although there's an Application class. Instead, an app is a loose collection of Activities. There are many good reasons for this state of affairs; for example, it supports fast app switching and easy interaction between Activities of different apps.
The best way to coordinate your "app" so that one Activity doesn't try to do something that's already been done is to use SharedPreferences to store app state. Nearly every other way of doing it is less preferred. Even if the system kills off your entire app, SharedPreferences will maintain the current state. The Application object won't.
Also, Android is based on pausing and resuming. An Activity or activities are created, pause, and resume. They may be destroyed, but that's an extreme case. A corollary to this is that apps should not have an exit button; there's no need for one. I sometimes see apps that have one, but what they're really trying to do is shut down a background Service or process. The best way to do that is to have an affordance that says "Sleep" or similar.
Have all activities inherit from the same hierarchy and put whatever you want in OnCreate, OnPause, OnResume, OnStop, OnDestroy and call the super where applicable.
Example
Parent
IamTheParentActivity : Activity
protected void onCreate()
{
setApplicationState(ApplicationState.Running);
}
protected void onPause()
{
setApplicationState(ApplicationState.Paused);
}
private void setApplicationState(Enum ApplicationState)
{
//Some Application Level Variable
Application.State = ApplicationState
}
Children
IamTheChild : IamTheParentActivity
protected void override onCreate()
{
base.OnCreate;
do other stuff
}
I have an issue. For analytic purposes I need to track when the APP (not activity) is resumed. The problem I have now is that if I put the tracker on the OnResume event of an activity, it will get fired every time the user goes back and forth on different activities.
How can I avoid that? How can I track the real "Application Resume," (when user actually exits the app and come back) and not the activity resume?
Any ideas is greatly appreciated. Thanks.
I encountered the same problem and solved it by creating base activity :
public class mActivity extends Activity{
public static final String TAG = "mActivity";
public static int activities_num = 0;
#Override
protected void onStop() {
super.onStop();
activities_num--;
if(activities_num == 0){
Log.e(TAG,"user not longer in the application");
}
}
#Override
protected void onStart() {
super.onStart();
activities_num++;
}
}
all the other activities in my app inherited mActivity. When an activity is no longer visible than onStop is called. when activities_num == 0 than all activities are not visible (meaning the the user close the app or it passed to the background). When the user start the application (or restarting it from the background) onStart will be called (onStart is called when the activity is visible) and activities_num > 0. hopes it helps...
Use the Application object of your app (see http://developer.android.com/reference/android/app/Application.html). If you create a custom Application class and configure it in your AndroidManifest.xml file you can do something like this:
Start tracking in the onCreate() of the Application object.
Instrument all your Activities so their onPause() and onResume() methods check with the Application object and see if they are the first Activity to run, or if they are continuing a previously running instance of the app.
Stop tracking in the onDestroy() of the Application object.
To a certain degree most of the analytics packages (Flurry and their ilk) do something similar to this. You'll need to do a little state machine work to get this to work right, but it shouldn't be too complicated.
Instead of OnResume(), hook into the OnCreate() event of your main activity.