1

In graph i plot value from calling api. I have display this graph in fragment class and when I change the orientation app will crash. How can I change orientation and display image in landscape mode without crashing an app.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
user2273146
  • 1,602
  • 2
  • 14
  • 27
  • Can you share the error message you get when the app crashes? – Rachit Sep 21 '16 at 04:55
  • @Rachit the terminal provide this error Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference. – user2273146 Sep 21 '16 at 04:57

2 Answers2

13

When the orientation change then the state of fragment will be changed. You need to save the state of your fragment by calling

setRetainInstance(true);

in onCreate() method.

You can also use onRestoreInstanceState() method which will store your savedInstanceState

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    onCreate(savedInstanceState);
}

The best solution is just add

android:configChanges="orientation|screenSize|keyboardHideen"

for your activity which holds the fragment in your manifest file.

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • Worked for me: android:configChanges="orientation|screenSize|keyboardHideen" My app was crashing when I tried to change orientation. The log keeps trying to say something like: TypeError: _reactNative.DeviceEventEmitter.removeListener is not a function. (In '_reactNative.DeviceEventEmitter.removeListener('didUpdateDimensions', this.handleDimensionsUpdate)', '_reactNative.DeviceEventEmitter.removeListener' is undefined) This error is located at: in ReactNativeModal (at CustomisableAlert.js:121) – Lucas Dias Procópio Mercês Jul 25 '23 at 14:38
1

During orientation changes, android destroys your activity. Maybe you set something and it became invalid during that change. You can read more about it here: https://developer.android.com/guide/topics/resources/runtime-changes.html

You said you want to be able to change the orientation. Just for completeness, you can fix the orientation to landscape or portrait, see: Android - disable landscape mode?

Community
  • 1
  • 1
Cubicle257
  • 335
  • 3
  • 14