androidandroid-orientation

How to listen window rotation finished in android?


In my app, I need to do some action when screen orientation change. So I do following work:

//in AndroidManifest.xml
<!-- SomeSetting -->
<activity android:configChanges="orientation|keyboardHidden|screenSize"
          android:label="@string/app_name"
          android:name="MainActivity"
          android:theme="@android:style/Theme.NoTitleBar">

//in MainActivity.java
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        doSomething();
    }

Here is my question:
I want to call doSomething() "after" Orientation Animation(like rotate or crossfade) finish. But it will be call right after animation start.
Is there a listener or something can listen to this kind of animation finish?

Update 0223

FYI:

After my experiments, I found that OnConfigurationChange is not reliable enough. I will use View.addOnLayoutChangeListener to get attribute (View is the component which I want to read value from). Then I can make sure the value is after orientation. By the way, I still need to override OnConfigurationChange to avoid recreate whole view. You may google addOnLayoutChangeListener() to learn how to use it.


Solution

  • I guess, what do you want is your doSomeThing() method should be called when view is drawn on layout.

    So you need to use View.post() method to achieve that. Have a look at following ex:

    fab.post(new Runnable() {
            @Override
            public void run() {
                // do something here 
                //......
                //......
                Toast.makeText(MainActivity.this, "Orientation has changed.", Toast.LENGTH_SHORT).show();
            }
        });
    

    Here fab is a view on which you want to perform operation when it is drawn on screen (So u do not need to use TimerTask).

    So as you told you want to update SurfaceView when orientation is changed. You can use post method of surface view(like fab in above code).