Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity lifecycle - onCreate called on every re-orientation

I have a simple activity that loads a bitmap in onCreate. I find that if I rotate the device I can see from the logs that onCreate called again. In fact, because all instance variables are set to default values again I know that the entire Activity has been re-instantiated.

After rotating 2 times I get an FC because not enough memory can be allocated for the bitmap. (Are all instances of the activty still alive somewhere? Or does the GC not clean up fast enough?)

@Override public void onCreate(Bundle savedInstanceState) {     File externalStorageDir = Environment.getExternalStorageDirectory();     File picturesDir = new File(externalStorageDir, "DCIM/Camera");     File[] files = picturesDir.listFiles(new FilenameFilter(){         public boolean accept(File dir, String name) {             return name.toLowerCase().endsWith(".jpg");         }});     if (files.length > 0) {         Bitmap bm = BitmapFactory.decodeStream(new FileInputStream(files[0]));         ImageView view = (ImageView) findViewById(R.id.photo);         view.setImageBitmap(bm);     } } 

From all that I read, onCreate should be called once during the lifetime of an application. Am I wrong about this? How can re-orienting the device cause the activity to be recreated?

like image 257
Synesso Avatar asked Oct 01 '11 06:10

Synesso


People also ask

Is onCreate called when orientation changes?

First onPause(), onStop() and onDestroy() will be called simultaneously and after some time when the activity restarts the onCreate(), onStart() and onReume() methods will be called simultaneously. So when you change the orientation of a screen the Activity will be restarted.

What is onCreate activity lifecycle?

onCreate() It is called when the activity is first created. This is where all the static work is done like creating views, binding data to lists, etc. This method also provides a Bundle containing its previous frozen state, if there was one.

What happens in the onCreate () callback method?

onCreate() You must implement this callback, which fires when the system first creates the activity. On activity creation, the activity enters the Created state. In the onCreate() method, you perform basic application startup logic that should happen only once for the entire life of the activity.

When onPause of activity lifecycle is called?

An activity can frequently transition in and out of the foreground—for example, onPause() is called when the device goes to sleep or when a dialog appears. Because this state can transition often, the code in these two methods should be fairly lightweight in order to avoid slow transitions that make the user wait.


2 Answers

android:configChanges="keyboardHidden|orientation|screenSize" 

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

http://developer.android.com/guide/topics/resources/runtime-changes.html

like image 69
achellies Avatar answered Sep 24 '22 20:09

achellies


Activity is recreated after each rotation by default. You can override this behaviour with configChanges attribute of the activity tag in AndroidManifest. For further details and different options, see http://developer.android.com/guide/topics/resources/runtime-changes.html

like image 31
Ash Avatar answered Sep 21 '22 20:09

Ash