Skip to content Skip to sidebar Skip to footer

Android - Screen Orientation Reloads Activity

im going to re-ask this question because there is SO much bad information out there its really depressing. the short story is that i dont want anything to change or happen when the

Solution 1:

The solution using android:configChanges="orientation" won't work unless the application has an API level of 12 or lower.

In API level 13 or above, the screen size changes when the orientation changes, so this still causes the activity to be destroyed and started when orientation changes.

Simply add the "screenSize" attribute like I did below:

<activity>
    android:name=".YourActivityName"
    android:configChanges="orientation|screenSize">
</activity>

Now, when your change orientation (and screen size changes), the activity keeps its state and onConfigurationChanged() is called. This will keep whatever is on the screen (ie: webpage in a Webview) when the orientation chagnes.

Learned this from this site: http://developer.android.com/guide/topics/manifest/activity-element.html

Also, this is apparently a bad practice so read the link below about Handling Runtime Changes:

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

Solution 2:

A very similar question is here: Don't reload application when orientation changes

Anyway, first of all you should change the code from onCreate to another method and divide it by "create random number and load image from the net" and "set image to view".

You have a Bitmap on the activity, that you verify if it's null everytime the activity starts.

If it is, do all. If it's not, just set the ImageView or whatever with the Bitmap you have.

To avoid destroying the Bitmap when it rotates use:

Bitmap image = null;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 image = (Bitmap) getLastNonConfigurationInstance();

     if(bitmap == null){
         image = downloadImage();
      }
     setImage(bitmap);
}


@OverridepublicObjectonRetainNonConfigurationInstance() {
    return bitmap;
}

This works, i'm 100% sure.

Solution 3:

For example, add to the manifest:

<activityandroid:name=".Explorer"android:label="@string/app_name"android:configChanges="orientation"android:launchMode="standard"></activity>

and then just override the default function:

@OverridepublicvoidonConfigurationChanged(Configuration _newConfig){
            super.onConfigurationChanged(_newConfig);
intheight= getWindowManager().getDefaultDisplay().getHeight();
        intwidth= getWindowManager().getDefaultDisplay().getWidth();
        if(width > height){
// layout for landscape
}else{
// layout for portrait
        }
            }

NOTE: if you override this function YOU must redo the layout yourself. That is the only way to do what you want. Android's approach to fixing your screen size is to blow the entire Activity away and bring it up again with the new dimensions. If you opt to not have it blow the Activity away then YOU have to handle the screen size change. There aren't any other options to that.

EDIT: In this particular app, I have a table that shows up in portrait, and a chart that shows up in landscape. So I do this:

voidshowDataView()
    {
        setContentView(mainView);
        currentView = mainView; 
    }

    voidshowGraphView()
    {
        if(currentView == mainView){
            if(graphView == null){
                setContentView(R.layout.graphview);
                graphView = (GraphView)findViewById(R.id.graphview);
            }
            setContentView(graphView);
            currentView = graphView;
            graphView.setDataToPlot(DATA_TO_PLOT);
            graphView.clear();
        }
    }

@OverridepublicvoidonConfigurationChanged(Configuration _newConfig){
        super.onConfigurationChanged(_newConfig);
        int height = getWindowManager().getDefaultDisplay().getHeight();
        int width = getWindowManager().getDefaultDisplay().getWidth();
        if(width > height){
            showGraphView();
        }else{
            showDataView();
        }
        if(graphView != null) graphView.setGraphWidth(width);
    }

Everytime the orientation changes, I switch the content view from one to the other. In your case you might be able to just invalidate the current root View and see if it will redraw correctly.

This is why you want the platform to take care of it, so you don't deal with this crap. You'd be better off just using onSaveInstanceState to preserve the specific data you need and then check in onCreate for that data and then use it. See http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges for info about that.

Solution 4:

what worked for me is declaring one of the class variables as static. it turns out that static class variables are initialized once when the application is being loaded, but when the application is being reloaded, these won't be initialized again.

these variables could hold a state or an object that you don't want to reinitialize. I can't say that this is not super elegant, but tested and worked for me.

Solution 5:

Save the image details in your onPause() or onStop() and use it in the onCreate(Bundle savedInstanceState) to restore the image.

More info on the actual process is detailed here http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle as it is different in Honeycomb than previous Android versions.

Post a Comment for "Android - Screen Orientation Reloads Activity"