Android: manual screen orientation without restarting activity?

I need to make an application that plays video with a button for full-screen video viewing. The button is used to manually switch between landscape and portrait display. We do not want automatic rotation detection. Thus, the manifest file is installed 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 action - the onCreate () call was called. Thus, video playback starts from the beginning unexpectedly. I cannot make it smooth, as when using onConfigurationChanged() , a method for automatically detecting rotation.

So how to change the screen orientation manually without restarting?

Thanks.

+8
source share
6 answers

To change the orientation of the manual:

 <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); } } 

To determine auto orientation:

 <activity android:name=".VideoDetails" android:configChanges="orientation"/> public class VideoDetails extends Activity { ... onConfigurationChanged(Configuration newConfig){ // do stuff for orientation change. } } 
+6
source

http://developer.android.com/guide/topics/resources/runtime-changes.html You can see the link under the heading Processing configuration changes.

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

Now that one of these configurations changes, MyActivity does not restart. Instead, MyActivity receives an onConfigurationChanged () call. This method is passed to the Configuration object, which defines 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 up to 5 minutes and the orientation changes, you know that he played up to 5 minutes.

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

When the activity of a change in orientation is destroyed and recreated. If you want to save data and save, you can do as below to save a large data set.

  @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 a small data set

  @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 the 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 on Android . In this case, the video is also transmitted from the server. Check the accepted answer (answer to the community call). Similarly, I suggested.

+2
source

Start by adding android:configChanges node to the android:configChanges node manifest

 android:configChanges="keyboardHidden|orientation" 

For more information check this link.

+1
source

Add tag

 "android:configChanges="keyboard|keyboardHidden|orientation|screenSize|navigation" for your activity in Manifest.xml, 

And override the method

 public void onConfigurationChanged(Configuration newConfig) {} 

If you need to change the layout to a changechange chek, replace the layout with a change in orientation

0
source

Restarting the application after changing orientation is inevitable.

Save the variable related to the video status in the application so that the activity does not resume playback at the beginning.

This code is from an example application that I made to verify this answer.

 public class MyApplication extends Application { int counter; @Override public void onCreate() { super.onCreate(); counter = 0; } } 

Add the application to the manifest

 <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:name="com.example.orientationchange.MyApplication" > <activity ... 

Using:

 public class MainActivity extends Activity { TextView output; MyApplication app ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = (MyApplication) getApplication(); setContentView(R.layout.activity_main); output = (TextView) findViewById(R.id.output); //A counter thread used to test the solution. new CounterTask().execute( 100 ); } ... 

Now rotation does not clear state variables.

Alternatively, you can use Preferences to more efficiently resolve code to maintain state. Preferences will also remember state from one use of the application to another, where the state of the application is cleared onDestroy () of the application.

0
source

I searched for a solution for a very long time, and for me the definition of orientation in the manifest as blocked worked:

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

This prevents the recreation of the Activity even if you call setRequestedOrientation ()

0
source

All Articles