0

I created an app that reads books to the user.

The user needs to be able to continue reading from the same position as he was in a previous app session.

How do i do something like that, if a process kill can occur anytime?

DennisVA
  • 2,068
  • 1
  • 25
  • 35
  • save your data in OnStop() method and It will always be called – Sultan Mahmud Apr 18 '19 at 11:25
  • Possible duplicate of [Save the position of scrollview when the orientation changes](https://stackoverflow.com/questions/29208086/save-the-position-of-scrollview-when-the-orientation-changes) – Vivek Mishra Apr 18 '19 at 11:26
  • 1
    @SultanMahmud `OnStop()` is not guaranteed to call , `OnPause()` is the last method which is guaranteed to get called – Manohar Apr 18 '19 at 11:33
  • You can use [`ProcessLifecycleOwner`](https://developer.android.com/reference/android/arch/lifecycle/ProcessLifecycleOwner) for this purpose . What exactly you are saving when process getting killed ? – ADM Apr 18 '19 at 11:49

1 Answers1

2

you can create here a service class in your app like this and create an api with your backend too :-

Service Class OnClearFromRecentService.class:-

public class OnClearFromRecentService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {

        callApi();
        stopSelf();
    }
    public void callApi(){

     //set your api here
    }
}

and set this service class in your manifeast :-

<service
            android:name=".halper.OnClearFromRecentService"
            android:stopWithTask="false" />

and start this service when your video is play :-

OnClearFromRecentService onClearFromRecentService = new OnClearFromRecentService();

or stop your service where your you need to stop service like this:-

onClearFromRecentService.onTaskRemoved(new Intent(LandingScreen.this, OnClearFromRecentService.class));
Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17