My Android app crashes when I change the screen orientation

In my application, network access works in a stream. Whenever I change the screen orientation, my application crashes. How can i solve this?

+3
source share
4 answers

Thanks for the answers .. I have a solution using the OnStop implementation lifecycle method. In this method, I just added the code below

@Override
public void onStop()
{
    super.onStop();
    if(thread!=null)
        thread.stop();
    if(dialog!=null)
        dialog.dismiss();
}
+2
source

Activity actually stops and restarts with every change in device orientation. You need to write your stream with this in mind, i.e. Stop the stream and restart it when the device changes orientation, possibly preserving the state between them.

+1
source

, , .

, , OnCreate(),

.

Best regards, ~ Anup

+1
source

you can make the application listen for events that will lead to changes related to restarting - such as the orientation and visibility of the keyboard - and process them as part of your work.

Start by adding android:configChangesnode to the node activity manifest

 android:configChanges="keyboardHidden|orientation"

Then, as part of the Activity, override the onConfigurationChanged method and call setContentView to force the GUI layout to be redone in the new orientation.

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.myLayout);
}
+1
source

All Articles