Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-10863

Android: Application restarts when orientation changes and targetSdkVersion is 13 or higher



      If an app targets Android api level 13 (Honeycomb MR2, aka Android 3.2) or higher, the screenSize config change (which occurs when the orientation changes, for example) is not handled automatically by Android, and thus causes (by default) an activity restart (cf http://developer.android.com/reference/android/R.attr.html#configChanges). We don't accept activity restarts in Titanium and instead just restart the app (i.e., close all activities and restart the root activity). Traditionally we overcome the problem with orientation changes using the configChanges attribute's orientation value in our Activity entries in AndroidManifest.xml. But now screenSize also needs to be handled.

      So if an app developer changes their tiapp.xml to add android:targetApiLevel="13" (or higher) – which is something they would do to get an ICS look-and-feel, for example, and will therefore occur more and more – their app will restart (on 3.2 or higher devices) when the orientation changes because of the unhandled screenSize. So we need to add screenSize to the configChanges in AndroidManifest.xml Activity entries. However, it's not that easy: to do that, we need be sure to also build against api 13 or higher, since building against an older version will cause aapt to fail if screenSize is in there; this fails the application build. So a somewhat clever solution is required.

      Fail Case / Test Case

      • Create a Titanium mobile app.
      • Change its tiapp.xml so that the <android> section looks like this:

      <android xmlns:android="http://schemas.android.com/apk/res/android">
      		<uses-sdk android:targetSdkVersion="13" android:minSdkVersion="8"/>

      • Replace the app.js with this:


      var win = Titanium.UI.createWindow({  
          title:'Root Window',
      	exitOnClose: true
      btn = Ti.UI.createButton({
      	title: "Open Heavyweight Window",
      	left: "16dp", right: "16dp", height: "48dp",
      	bottom: "16dp"
      	text: "This is the root window",
      	color: "#000",
      	height: "48dp"
      btn.on("click", function() {
      	var w = Ti.UI.createWindow({
      		backgroundColor: "#f00",
      		title: "New Activity",
      		fullscreen: false
      		text: "This is a new Activity",
      		height: "48dp",
      		color: "#fff"

      • Run the app on 3.2 or higher device.
      • After the app loads, click the "Open Heavyweight Window" button.
      • After the red window opens, turn the device to a different orientation (i.e., from portrait to landscape or vice-versa).
      • In the fail case, the app will actually close completely and re-open back at the first (white, not red) window. When testing the fix, the app should not close and the red window should still be showing.
      • Also build and run the app on a pre-3.2 device (a 2.3 phone, for example) to be sure nothing is broken for older versions of android.


          Issue Links



              • Assignee:
                pwang Ping Wang
                billdawson Bill Dawson
              • Watchers:
                0 Start watching this issue


                • Created:

                  Backbone Issue Sync

                  • Titanium SDK/CLI <> Titanium Mobile
                    Synced with:
                    Sync status:
                    Last received:
                    Last sent:

                    Git Integration