Activity indicator whilst capturing screen

I have a button that calls display.captureScreen(true). In the simulator the image is saved almost instantly, however on the device it can take 5 to 10 seconds. I would like to show a modal activity indicator so that the user won’t end up tapping the button multiple times.

The following code works in the simulator, however on the device it leaves the activity indicator spinning, meaning that I have to force-quit.

Can someone please assist?

function saveScreenshot()         capture = display.captureScreen(true)    capture:removeSelf()    capture = nil end function onButtonPress(event) native.setActivityIndicator(true) saveScreenshot() native.showAlert("My App", "Screenshot saved", {"OK"}) native.setActivityIndicator(false) end

display.captureScreen() saves whole screen as expected. If you want you can save only certain display group  or object - if so use display.capture() - always check api :slight_smile:

I checked the API and I can capture the screen just fine - I think you need to re-read my question!

I’ve had problems in the past with native.setActivityIndicator when setting to both true and false in the same code block.

Try adding the native.setActivityIndicator(false) function to the end of your saveScreenshot() function, perhaps with a timer.performWithDelay just to be sure:

function saveScreenshot() capture = display.captureScreen(true) capture:removeSelf() capture = nil timer.performWithDelay(100, function() native.setActivityIndicator(false) end) end function onButtonPress(event) native.setActivityIndicator(true) saveScreenshot() native.showAlert("My App", "Screenshot saved", {"OK"}) end

Still no luck I’m afraid. The native call to showAlert always triggers at the correct time (saves the screenshot and then the alert fires). I don’t see why the activityIndicator isn’t being displayed until after the alert message

It could be that the 2 native objects clash with each other. If you’ve ever tried to call an alert when there is already an alert on screen you get at best something weird happening and at worst the app crashing.  

So perhaps you cannot show these 2 native objects together, and so the OS queues the second one?

I searched about a bit and found a blog post (why is this not in the API Docs!). It’s best to use a timer to turn off the indicator. For example:

function onButtonPress() native.setActivityIndicator(true) timer.performWithDelay(100,function() captureScreen() native.setActivityIndicator(false) end)

I hope this helps someone else

And has that worked?

Yes :slight_smile:

display.captureScreen() saves whole screen as expected. If you want you can save only certain display group  or object - if so use display.capture() - always check api :slight_smile:

I checked the API and I can capture the screen just fine - I think you need to re-read my question!

I’ve had problems in the past with native.setActivityIndicator when setting to both true and false in the same code block.

Try adding the native.setActivityIndicator(false) function to the end of your saveScreenshot() function, perhaps with a timer.performWithDelay just to be sure:

function saveScreenshot() capture = display.captureScreen(true) capture:removeSelf() capture = nil timer.performWithDelay(100, function() native.setActivityIndicator(false) end) end function onButtonPress(event) native.setActivityIndicator(true) saveScreenshot() native.showAlert("My App", "Screenshot saved", {"OK"}) end

Still no luck I’m afraid. The native call to showAlert always triggers at the correct time (saves the screenshot and then the alert fires). I don’t see why the activityIndicator isn’t being displayed until after the alert message

It could be that the 2 native objects clash with each other. If you’ve ever tried to call an alert when there is already an alert on screen you get at best something weird happening and at worst the app crashing.  

So perhaps you cannot show these 2 native objects together, and so the OS queues the second one?

I searched about a bit and found a blog post (why is this not in the API Docs!). It’s best to use a timer to turn off the indicator. For example:

function onButtonPress() native.setActivityIndicator(true) timer.performWithDelay(100,function() captureScreen() native.setActivityIndicator(false) end)

I hope this helps someone else

And has that worked?

Yes :slight_smile: