Search code examples
androidandroid-activityandroid-lifecycle

Deleting a file before user swipes to remove the app


I have 2 activities SingActivity and ExoPlayerActivity. In SingActivity, user records a song and the song is saved in internal storage. As soon as the song is saved, I am opening ExoPlayerActivity to play the song. The ExoPlayerActivity has a save button.

If user presses save button, then I am doing nothing as song is already saved after recording. If user does not press save button and exists from ExoPlayerActivity, I am deleting the file which was saved. Suppose user is in ExoPlayerActivity and user puts the app in background and swipes the app from background, then too I want to delete the file as saved is not pressed.

So my question is where to put the delete code? I can't put in onDestroy or in finish() as they are not called every time and may cause problem in case of resource constraint scenario.


Solution

  • With this approach it will be really buggy (in case if it was even possible) i suggest that you use android cache to do this:

    refer to Android developers| Save files on device storage

    or you can see this answer on stackoverflow: Creating temporary files in Android