Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: manual screen orientation without restarting the activity?

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.

like image 920
tim Avatar asked Mar 26 '13 08:03

tim


People also ask

Can you rotate android screen manually?

When Auto-rotate screen is turned off, you can still rotate the screen manually. Tap the rotate icon that appears in the bottom-right corner. You can also turn Auto-rotate screen on and off by swiping down twice from the top of the screen and tapping the button in the Quick Settings menu.

How do I stop android from restarting activity when changing orientations?

Prevent Activity to recreated Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.


3 Answers

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.
    }
}
like image 95
tim Avatar answered Oct 26 '22 03:10

tim


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.

like image 27
Raghunandan Avatar answered Oct 26 '22 02:10

Raghunandan


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

android:configChanges="keyboardHidden|orientation"

For more information check this link

like image 29
Janmejoy Avatar answered Oct 26 '22 01:10

Janmejoy