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

    Details

      Description

      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">
      	<manifest>
      		<uses-sdk android:targetSdkVersion="13" android:minSdkVersion="8"/>
      	</manifest>
      </android>
      

      • Replace the app.js with this:

      "app.js"

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

      • 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.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

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

                    Git Integration