Shane
Shane

Reputation: 972

Activity Orientation Change

Can I tell a different activity to change its orientation from the current activity using this method?

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

I am trying to do something like:

game.class.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

but this will not compile.

Upvotes: 0

Views: 272

Answers (2)

neo
neo

Reputation: 2042

When you run the next activity, send extra information on the intent that you use to start the next activity with and in next Activity's onCreate change the orientation according to the information passed.

Example

Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("Orientation", "landscape");
startActivity(intent)

In the next Activity do the following

Intent intent = getIntent();
String orientation = intent.getStringExtra("Orientation")

android:configChanges="orientation|keyboardHidden" do this in manifest

Use onConfigurationChanged method of activity to detect Orientation Change

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

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

}

}

Upvotes: 0

Helmisek
Helmisek

Reputation: 1319

Edit. Just transfer the string for example orientation with the selected value. And build it using constructor in the another class. Then in the onCreate you can write if(this.orientation == "Landscape){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }

Hope it helps you.

Upvotes: 1

Related Questions