applicationResume loses orientation

After resuming an application, I’m trying to show the user a pause screen over the game. When the application resumes from being suspended, the display contentWidth and contentHeight have been reset. That is, an app only in landscapeRight will resume from ‘suspend’ into portrait mode. This has the strange effect of typically rendering the existing storyboard scene correctly and rendering the overlay with the correct orientation but chopped off because of the current values of contentWidth and contentHeight.

To reproduce this bug, use the following config, build, and main. Execute the app then suspend the device, then hit rotate left or right, then resume. You should see the contentWidth and height swap from suspend to resume. Rotating and then suspending causes the problem in my real app but not in this sample app so please suspend then rotate here. :slight_smile:

--Build.settings  
settings = {  
 orientation = {  
 default = "landscapeRight",  
 supported = {  
 "landscapeRight",  
 },  
 },  
  
 iphone = {  
 plist = {  
 UIApplicationExitsOnSuspend = false,  
 },  
 }  
}  

[lua]–config.lua
application =
{
content =
{
width = 320,
height = 480,
scale = “letterbox”,
imageSuffix =
{
["-x2"] = 1.8,
},
}
}[/lua]

[lua]local storyboard = require(‘storyboard’)
local function main()
local onSystem = function(event)
if event.type == “applicationSuspend” then
print(display.contentWidth, display.contentHeight)
elseif event.type == “applicationResume” then
print(display.contentWidth, display.contentHeight)
end
end
Runtime:addEventListener(“system”, onSystem)
end

main()[/lua]

I have tried repeatedly to find any information about this problem using the forums and been unable to effectively search the forums. All the links generated directly from the search or from a site-specific Google search no longer point to the original content. If this issue has been addressed before, will someone please provide an active link and method for accessing old forum content? Thanks in advance! [import]uid: 168249 topic_id: 30796 reply_id: 330796[/import]

After reading previously that the device orientation could not be forced, it was time to go ahead and try it anyways. When the application resumes from suspend, it IS actually possible to force the contentWidth and contentHeight settings.

[lua]local onSystem = function(event)
if event.type == “applicationSuspend” then
print(display.contentWidth, display.contentHeight)
elseif event.type == “applicationResume” then
display.contentWidth, display.contentHeight = 480, 320
print(display.contentWidth, display.contentHeight)
end
end
Runtime:addEventListener(“system”, onSystem)[/lua]

Even using a timer to wait until the system ‘stabilized’ and discovered the supported orientation didn’t work. The quickest way to make the app respond on resume was to manually override the content values. Here’s hoping this helps someone else with similar problems! [import]uid: 168249 topic_id: 30796 reply_id: 123283[/import]

After reading previously that the device orientation could not be forced, it was time to go ahead and try it anyways. When the application resumes from suspend, it IS actually possible to force the contentWidth and contentHeight settings.

[lua]local onSystem = function(event)
if event.type == “applicationSuspend” then
print(display.contentWidth, display.contentHeight)
elseif event.type == “applicationResume” then
display.contentWidth, display.contentHeight = 480, 320
print(display.contentWidth, display.contentHeight)
end
end
Runtime:addEventListener(“system”, onSystem)[/lua]

Even using a timer to wait until the system ‘stabilized’ and discovered the supported orientation didn’t work. The quickest way to make the app respond on resume was to manually override the content values. Here’s hoping this helps someone else with similar problems! [import]uid: 168249 topic_id: 30796 reply_id: 123283[/import]

The above post regarding the resolution of the issue was premature. There are still some problems getting Android 4.0.4 to resume and are detailed in another thread. In the aforementioned suspend/resume case, resetting the contentWidth and contentHeight are not always sufficient to get the screens to redraw correctly. For example, a black rectangle is created to cover the whole screen as a background in the pause menu. When the application resumes in portrait instead of landscapeRight, the contentWidth and contentHeight are reset but the rectangle dimensions are not correctly reset. The following code has helped by creating a delay and removing all scenes; however, the ability to resume properly into a running app seems to be a consistent problem.

[lua]local onSystem = function(event)
if event.type == “applicationSuspend” then
storyboard.removeAll()
elseif event.type == “applicationResume” then
display.contentWidth, display.contentHeight = 480, 320
local current = storyboard.getCurrentSceneName()
tnt:newTimer(200, function()
storyboard.gotoScene(“reset”)
if current == “play” then
storyboard.showOverlay(“pauseMenu”)
end
end, 1)
end
end[/lua]

Here, the reset function is just a scene used for resetting a level when the user dies. It simply removes the previous scene and redraws it. The ‘tnt’ is from the wonderful pausable transitions library and could just as easily be a regular timer function for the same results.

[lua]function scene:enterScene(event)
local group = self.view
local prior = storyboard.getPrevious()
storyboard.removeScene(prior)
storyboard.gotoScene(prior, “crossFade”, 20)
end

scene:addEventListener(“enterScene”, scene)[/lua]

Please do not let my previous post serve as the final answer to others who have similar problems because this one is not quite fixed. I welcome the input of anyone who has been more successful with application resume or has seen similar problems. [import]uid: 168249 topic_id: 30796 reply_id: 123813[/import]

The above post regarding the resolution of the issue was premature. There are still some problems getting Android 4.0.4 to resume and are detailed in another thread. In the aforementioned suspend/resume case, resetting the contentWidth and contentHeight are not always sufficient to get the screens to redraw correctly. For example, a black rectangle is created to cover the whole screen as a background in the pause menu. When the application resumes in portrait instead of landscapeRight, the contentWidth and contentHeight are reset but the rectangle dimensions are not correctly reset. The following code has helped by creating a delay and removing all scenes; however, the ability to resume properly into a running app seems to be a consistent problem.

[lua]local onSystem = function(event)
if event.type == “applicationSuspend” then
storyboard.removeAll()
elseif event.type == “applicationResume” then
display.contentWidth, display.contentHeight = 480, 320
local current = storyboard.getCurrentSceneName()
tnt:newTimer(200, function()
storyboard.gotoScene(“reset”)
if current == “play” then
storyboard.showOverlay(“pauseMenu”)
end
end, 1)
end
end[/lua]

Here, the reset function is just a scene used for resetting a level when the user dies. It simply removes the previous scene and redraws it. The ‘tnt’ is from the wonderful pausable transitions library and could just as easily be a regular timer function for the same results.

[lua]function scene:enterScene(event)
local group = self.view
local prior = storyboard.getPrevious()
storyboard.removeScene(prior)
storyboard.gotoScene(prior, “crossFade”, 20)
end

scene:addEventListener(“enterScene”, scene)[/lua]

Please do not let my previous post serve as the final answer to others who have similar problems because this one is not quite fixed. I welcome the input of anyone who has been more successful with application resume or has seen similar problems. [import]uid: 168249 topic_id: 30796 reply_id: 123813[/import]