AndroidM
AndroidM

Reputation: 421

how to implement onRetainNonConfigurationInstance

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

Upvotes: 8

Views: 16177

Answers (4)

Arman
Arman

Reputation: 55

onRetainCustomNonConfigurationInstance from AppCompatActivity
onRetainNonConfigurationInstance from Activity
If we want to save data

data class A(val data: Int)

AppCompatActivity

override fun onRetainCustomNonConfigurationInstance(): Any {
    return A(0)
}

onCreate

val a = lastCustomNonConfigurationInstance as? A

If we want to save the object and data.
the object A will live when the screen is flipped.
the object A will be destroyed if the user destroys the screen.

class A private constructor() {

    var data: Int = 0
    var isConfiguration: Boolean = false

    companion object {
        private var instance: A? = null

        fun getInstance(
            data: Int,
            isConfiguration: Boolean
        ): A {
            synchronized(this) {
                if (instance == null) {
                    instance = A()
                }
                instance!!.data = data
                instance!!.isConfiguration = isConfiguration
                return instance!!
            }
        }

        fun clear() {
            synchronized(this) {
                instance?.apply {
                    if (!isConfiguration) {
                        instance = null
                    } else isConfiguration = false
                }
            }
        }
    }
}

AppCompatActivity

override fun onRetainCustomNonConfigurationInstance(): Any {
    return A.getInstance(0, true)
}

onCreate

val a = lastCustomNonConfigurationInstance as? A

onDestroy

A.clear()

Upvotes: 0

Den
Den

Reputation: 170

In my media player, in order not to re-create MediaPlayer completely, I did the following:

1) In AndroidManifest.xml added

<activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize">

2) Inside the MainActivity added

 @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setSize(mVideoWidth, mVideoHeight);
}

I hope this helps someone.

Upvotes: 0

Alexander
Alexander

Reputation: 48272

Never mind that it's deprectated, it works fine. Simplest would be:

public Object onRetainNonConfigurationInstance() {
   return this;
}

Then in YourActivity's onCreate()

public void onCreate(Bundle savedState)
{
   YourActivity prevActivity = (YourActivity)getLastNonConfigurationInstance();
   if(prevActivity!= null) { 
       // So the orientation did change
       // Restore some field for example
       this.myValue = prevActivity.myValue;
   }
}

Upvotes: 3

JorganPubshire
JorganPubshire

Reputation: 807

I believe that onRetainNonConfigurationInstance() is deprecated. It will tell you to use Fragments instead. Here is a link to the Fragment documentation. Basically, you will put your UI and data into a custom Fragment, then use the FragmentManager to store an instance of your Fragment. Then, when the activity restarts, you can fetch your Fragment and reposition as needed.

Upvotes: 7

Related Questions