Ebrahim Tahernejad
Ebrahim Tahernejad

Reputation: 484

Android : Activity restarts when changing the orientation

I have problem with my activity when I change the phone's orientation, the activity runs the onCreate method agaian (and my program downloads a file in onCreate)

how can I make activity not restart after orientation change ?

Upvotes: 0

Views: 133

Answers (3)

marcinj
marcinj

Reputation: 49976

You should properly handle you configuration changes, you can use onSaveInstanceState to store information that given file was already downloaded - ie. with path to it. In onCreate Bundle savedInstanceState will be non null after configuration change, you can read information on your downloaded file from it.

If you will use android:configChanges, then it will not solve your problem. Once you go to other app, android might destroy your activity, when you will go back to your app, android will recreate it again - and you again will start downloading your file.

Upvotes: 1

Peter Moskala
Peter Moskala

Reputation: 360

android:configChanges is good, if you're not changing anything, when orientation of activity will be changed.

You can also read about handling orientation change in documentation:this and this

Upvotes: 1

FD_
FD_

Reputation: 12919

This is the default behaviour for Android Activities - They get recreated whenever you change the device configuration.

To change this behaviour, add this line to the Activity declaration in your AndroidManifest.xml:

android:configChanges="orientation|keyboardHidden|screenSize" 

This will tell the system that you handle orientation changes for your Activity on your own.

Upvotes: 3

Related Questions