- Home /
The question is answered, right answer was accepted
HELP with OnApplicationQuit().. ANDROID
Hi!
I tried to look for the answer online but no one helps me :(
My problem is i want to save PlayerPrefs when the player close the application not using my game exit button. It doesnt save the data in Android. But it works perfectly in Unity editor. Please help thanks.. btw sorry for bad english.
here's my code:
void OnApplicationQuit()
{
PlayerPrefs.SetInt("Score", cur_score);
PlayerPrefs.Save();
}
Btw way my game goes like this.
In the Start() i get the value of plyerprefs score
public int cur_score = 0;
void Start() { //The purpose of this line is to get the Score value when the game starts so that if //the player stop the game i'll just reload this value on OnApplcaitionQuit(); //cos i dont want to save the changes in the PlyerPrefs(Score) until the player didnt //finish the level. cur_score = PlayerPrefs.GetInt("Score"); }
here:
void OnApplicationQuit()
{
PlayerPrefs.SetInt("Score", cur_score); PlayerPrefs.Save();
}
does this makes sense? :( Please help!! Sorry for bad english. Thank you so much!
I'm use windows platform, but I have the same problem when I running my project (not editor ) Any body can help me????
Hey @areejyahya your problem is different from the OP. Please post a new question, and add as much details as possible.
Answer by pmaloo · Sep 02, 2015 at 12:35 PM
This is how Android has been designed.
OnApplicationPause
corresponds to onPause/onResume
events of the activity.OnApplicationQuit
corresponds to onDestroy
event of activity lifecycle.
According to android docs, you are guaranteed to get a call back for onPause but anything after it may not be called. Therefore, you may not get a call to OnApplicationQuit
if Android destroys the activity after onPause
(Pre Honeycomb) or onStop
.
For more details on Android activity life cycle events, please visit http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle.
Answer by osum4est · Mar 27, 2015 at 04:39 PM
Hi! A few months too late... but I'm been having the same problem. Have you gotten OnApplicationExit() to work?
If not, just use OnApplicationPause(). That gets called whenever you switch to a different app or close the app!
Answer by ritesh_khokhani · Sep 02, 2015 at 10:39 AM
Hey,
With my experience playerprefs may/may not save when it is saved/changed in OnApplicationQuit()
White in case of OnApplicationPause() it is getting called suddenly!
Note: Experimented with Android platform only!