Android: manual screen orientation without restarting the activity?

10,826

Solution 1

For manaul orientation change:

<activity
    android:name=".VideoDetails"
    android:configChanges="orientation"/>

public class VideoDetails extends Activity {
    ...
    onStart() {
        setRequestedOrientation(orientation);
    }
    onConfigurationChanged(Configuration newConfig){
        // do stuff for orientation change.
    }
    onClick() {
        setRequestedOrientation(orientation);
    }
}

For auto-orientation detect:

<activity
    android:name=".VideoDetails"
    android:configChanges="orientation"/>

public class VideoDetails extends Activity {
    ...
    onConfigurationChanged(Configuration newConfig){
        // do stuff for orientation change.
    }
}

Solution 2

http://developer.android.com/guide/topics/resources/runtime-changes.html. You can have a looking at the link under the heading Handling the Configuration Change Yourself

<activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration.

 android:configChanges="orientation|screenSize" (andorid 3.2 and above screen size also changes. add this)

Suppose your video is 10 minutes. The video plays till 5 minutes and orientation changes, you know that it has played till 5 minutes.

You can save the actual progress of the video in onSaveInstanceState() and get the saved data in onRestoreInstanceState() from the Bundle, after that you can start the playing with either the progress data, or from the beginning if there was no data saved.

When orientation changes activity is destroyed and recreated. If you want to save data and retain you can do as below for saving large set of data

 @Override
 public Object onRetainNonConfigurationInstance() {
 final MyDataObject data = collectMyLoadedData();
 return data;
 }


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
    data = loadMyData();
}

}

For small set of data

 @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {
 super.onSaveInstanceState(savedInstanceState);
 // Save UI state changes to the savedInstanceState.
 // This bundle will be passed to onCreate if the process is
 // killed and restarted.
 savedInstanceState.putString("NICK_NAME", Name);//save nick name
 }

 @Override
 public void onRestoreInstanceState(Bundle savedInstanceState) {
 super.onRestoreInstanceState(savedInstanceState);
 // Restore UI state from the savedInstanceState.
 // This bundle has also been passed to onCreate.
 Name = savedInstanceState.getString("NICK_NAME");//restore nick name
 }

To check orientation

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}

VideoView in Android. In this case also video is streamed from the server. Check the answer accepted (commonsware answer). Exactly the same i suggested.

Solution 3

Start by adding the android:configChanges node to your Activity's manifest node

android:configChanges="keyboardHidden|orientation"

For more information check this link

Share:
10,826
tim
Author by

tim

Updated on June 04, 2022

Comments

  • tim
    tim almost 2 years

    I need to make an app playing video with a button for full-screen on the video. The button is used to manually switch between landscape and portrait of the video display. We don't want the auto rotation detect. So the Manifest file is set as below.

    <activity
        android:name=".VideoActivity"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden"/>
    

    I used

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); or setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

    to manually set the orientation. It works but it restarts the activity - the onCreate() was found called. So the video playback restarts from the beginning unexpectedly. I cannot make it smooth as like as using onConfigurationChanged() - the auto rotation detect method.

    So how to make manual screen orientation change without restarting the activity?

    Thank you.

  • tim
    tim about 11 years
    Thanks. Your method seems auto detect orientation change by the system. It can prevent from restarting the activity. But I don't want the auto detect. I need manual set.
  • Janmejoy
    Janmejoy about 11 years
    @user1835097 then you can do by overriding Activity.onConfigurationChanged
  • Janmejoy
    Janmejoy about 11 years
    @user1835097 check this link c0deattack.wordpress.com/2010/12/25/…
  • tim
    tim about 11 years
    Thanks. Your method of using onSaveInstanceState() and onRestoreInstanceState() is one of solutions. But my concern is the smooth of video playback, since my app playback video streaming from the server. The re-connection and video seek time would make the video playback discontinuous.
  • Raghunandan
    Raghunandan about 11 years
    @user1835097 check the last link. The user was also streaming videos from server. check the accepted answer.