i made a media player in android it's working great but when i change the screen orientation, the activity is being restarted i know that this question was already asked on stackoverflow several times but none of the answers helped me. i think i should use: onRetainNonConfigurationInstance
@Override
public Object onRetainNonConfigurationInstance() { ... }
but i didn't know the correct way to implement it so if someone could give me a tutorial or an implicit example i would be grateful
I believe that
onRetainNonConfigurationInstance()
is deprecated. It will tell you to useFragments
instead. Here is a link to theFragment
documentation. Basically, you will put your UI and data into a customFragment
, then use theFragmentManager
to store an instance of yourFragment
. Then, when the activity restarts, you can fetch yourFragment
and reposition as needed.In my media player, in order not to re-create MediaPlayer completely, I did the following:
1) In AndroidManifest.xml added
2) Inside the MainActivity added
I hope this helps someone.
Never mind that it's deprectated, it works fine. Simplest would be:
Then in
YourActivity
'sonCreate()