I have a question about life cycle.
I have 2 activities. (Just call it A and B).
In A which is MainActivity I have a button that call Activity B.
At activity B, pressing back button will call the finish().
The point is it's okay when I call finish(), wait a while and than call B again, it's okay.
But when I call finish() and immediately call B again than
previous B's onDestroy() is called after new B's onCreate(), onResume().
It's problem for me because I handle some static MediaPlayer on onResume(), onPause()... everywhere.
@Override
protected void onResume(){
super.onResume();
if (mediaPlayer != null)
mediaPlayer.start();
}
@Override
protected void onPause(){
super.onPause();
if(mediaPlayer != null)
mediaPlayer.pause();
}
@Override
protected void onDestroy(){
super.onDestroy();
if(mediaPlayer != null)
mediaPlayer.release();
mediaPlayer = null;
}
So if I press back and call B fast again, it resume the previous sound short and than stop.
Another class controls the mediaPlayer so I can't remove the static field.
I think I can handle this by waiting onDestroy().
Is there any way to make MainActivity(A) to wait until B is completely destroyed?
There is a
ActivityLifeCycleCallBack Listnerfor Application class, where you can know Which activity is created and which is Destroyed.This will help you to easily implement your current logic Application.ActivityLifecycleCallbacks