0

I'm need to save playing time to prefferensses when app is close but if i click on Task Manager and close app or switch to other and close it some time ago my app doesent save anything.

code of onStop

@Override
protected void onStop() {
    super.onStop();
    Log.d("", "MainActivity: onStop()");
    SearchFragment.sPref = MainActivity.this.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor ed = SearchFragment.sPref.edit();
    ed.putLong("SAVED_TIME", SearchFragment.mediaPlayer.getCurrentPosition());
    ed.commit();
}
  • _"...or switch to other and close it some time ago"_. Could you clarify it? – Onik Jan 24 '16 at 18:30
  • Yes, for example i open browser after i press home button and open my app, after it is i click on task manager button and switch to browser, look at some page, click to task manager again close my app and go back to browser, onPause doesent work ( – Nikolay Kolomiytsev Jan 24 '16 at 18:33
  • it is works only if i click BACK or HOME button, It looks like my app listening not a finish a back or home button pressing, why it is? – Nikolay Kolomiytsev Jan 24 '16 at 18:35
  • 1
    @NikolayKolomiytsev try to move this code to onPause() instead. And also I think it's better to call saving logic before calling super method. – Konstantin Loginov Jan 24 '16 at 18:37
  • _"onPause doesent work"_. How do you check it? – Onik Jan 24 '16 at 18:44
  • @Onik i check it in medialPlayer, this code save time of song and after app launching it start in this time, but it saves only if i press home or back, it is very sad. – Nikolay Kolomiytsev Jan 24 '16 at 18:48
  • Could you post the `onPause()` code? – Onik Jan 24 '16 at 18:51
  • @NikolayKolomiytsev no, it's not a piece of shit. What do you actually need? Moment, when process has been terminated? It can happen any time while app in the background by the system. onPause() is the method being called, once your activity is replacing by anything else. – Konstantin Loginov Jan 24 '16 at 18:52
  • @Onik if i post it **onPause()** app save my time if i it go to background and what happens if i close it after 1 minute for example? Yes, it save my time one minute ago, i need real time. – Nikolay Kolomiytsev Jan 24 '16 at 18:53
  • @NikolayKolomiytsev read about Android lifecycle please. You shouldn't care about the particular termination time of the app. More than that, you should use Service to play music in the background. And listen to it's lifecycle events, not activities one. – Konstantin Loginov Jan 24 '16 at 18:55
  • @KonstantinLoginov Service? Where i can found this? – Nikolay Kolomiytsev Jan 24 '16 at 19:05
  • 1
    @NikolayKolomiytsev http://developer.android.com/guide/topics/media/mediaplayer.html - you'd be surprise, but here :-) – Konstantin Loginov Jan 24 '16 at 19:08
  • @KonstantinLoginov, here it is? http://stackoverflow.com/a/8209975/4529775 – Nikolay Kolomiytsev Jan 24 '16 at 19:09
  • @NikolayKolomiytsev this question about playing music only while the app in the foreground (i.e. before onPause() being called). From your question in comments, it's clear, that you want your music being played even in the background – Konstantin Loginov Jan 24 '16 at 19:11

1 Answers1

1

If 'onPause' has been called and the Activity is still in the 'background', it wouldn't be called anymore. Life cycle of Activity: http://developer.android.com/images/activity_lifecycle.png

Maybe you need to print logs in 'onPause' and check when it will be called.

Yuni
  • 21
  • 6
  • OOO, understand :3 Ok, how to listen if my MediaPlayer stop working? – Nikolay Kolomiytsev Jan 24 '16 at 19:06
  • Don't know much about MediaPlayer. I have a (maybe naive) idea that you can start a thread or timer to record the progress every N seconds and save it somewhere... – Yuni Jan 24 '16 at 19:14