4

I have tried

android:screenOrientation="portrait"

and

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

but it is crashing the app, Is there any alternative method for this to work in Android 8.0.0+?

Logcat:

FATAL EXCEPTION: main
  Process: in.ajtech.finX, PID: 15077
  java.lang.RuntimeException: Unable to start activity ComponentInfo{in.ajtech.finX/in.ajtech.finX.CalendarActivity}: java.lang.IllegalStateException: Only fullscreen activities can request orientation
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
      at android.os.Handler.dispatchMessage(Handler.java:105)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6541)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
   Caused by: java.lang.IllegalStateException: Only fullscreen activities can request orientation
      at android.os.Parcel.readException(Parcel.java:1950)
      at android.os.Parcel.readException(Parcel.java:1888)
      at android.app.IActivityManager$Stub$Proxy.setRequestedOrientation(IActivityManager.java:5675)
      at android.app.Activity.setRequestedOrientation(Activity.java:5739)
      at in.ajtech.finX.CalendarActivity.onCreate(CalendarActivity.java:55)
      at android.app.Activity.performCreate(Activity.java:6975)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
      at android.os.Handler.dispatchMessage(Handler.java:105) 
      at android.os.Looper.loop(Looper.java:164) 
      at android.app.ActivityThread.main(ActivityThread.java:6541) 
      at java.lang.reflect.Method.invoke(Native Method) 
Pang
  • 9,564
  • 146
  • 81
  • 122
Habeeb Rahman
  • 405
  • 1
  • 5
  • 15

6 Answers6

4

BUG

Read Only fullscreen activities can request orientation

Only fullscreen activities can request orientation at android.app.ActivityThread.performLaunchActivity

You should use AppCompatActivity instead of Activity.

let your activity extend AppCompatActivity.

JAVA

public class YourActivity extends AppCompatActivity {
  // ...
}

Kotlin

class  YourActivity : AppCompatActivity()

FYI

Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar. However, app bar features have gradually been added to the native ActionBar over various Android releases. As a result, the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library's version of Toolbar, and they are available on any device that can use the support library.

From Setting Up the App Bar.

DEMO

Set your style

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

</style>
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • This is not a real solution. What if I want to have the ActionBar? Google should just fix this crash that they have newly introduced. – doctorram Mar 22 '18 at 14:13
1

It's android sdk(27) issue , you can't use portrait with Translucent so reduce your target sdk to 26 or remove Translucent theme or remove portrait mode.

Rajasekaran M
  • 2,478
  • 2
  • 20
  • 30
1

In AndroidManifest.xml make the following changes:

  1. For opaque activities, i.e. full-screen, set:

    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.NoTitleBar"
    

    Note that the theme should be NOT Translucent.

  2. For transclusent activities, i.e. pop-up dialogs etc, set:

    android:screenOrientation="unspecified"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    

    Note that screenOrientation is not specified here. You can use Translucent themes.

This works without downgrading SDK version.

Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87
0

You can set the property from you manifest file, inside each activity, add android:screenOrientation="portrait"

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Gordon developer
  • 387
  • 3
  • 13
0

Repacing extents Activity to AppCompatActivity fixed the issue. Thank you for all your help

Habeeb Rahman
  • 405
  • 1
  • 5
  • 15
0

I got the same error, when I tried to open activity as dialog inside another activity. then I removed android:screenOrientation="portrait" from dialog activity deceleration in my Manifest, and problem solved! this is because the parent activity should be responsible for the orientation.

Mojtaba
  • 205
  • 3
  • 10