0

do you know if it is possible to delay the onStop method once it has been fired?

I'm overriding the onStop() method, and I want to add a small delay before it calls super.onStop().

(I have an activity that displays a view -dialog with a surface holder- and when I close the activity, the view is still visible - something like 1s - while home screen or prior activity is already displayed)

I have multiple ways of closing the activity, and I don't want to implement the behaviour for each way, that's way putting it in the onStop() looks a good idea to me.

Thanks in advance. G.

  • Why do you want this delay? It rather sounds to me like from an user experience point of view, you would want the dialog with surface holder to disappear faster. Could you provide some example code to reproduce the problem? – Simon Forsberg Oct 23 '13 at 15:33

2 Answers2

0

No, this is not possible.

1) You cannot delay returning more than a handful of milliseconds in a UI thread method, without risking an Application Not Responding error.

2) You must call through to the superclass method before returning, failing to do so will generally generate an error.

To cause something to happen at a delayed time after a UI method, you would start a timer. However, onStop() is called when your Activity is ceasing visibility, so you don't really get to do anything visible at a later time, even if you manage to run some code then. Also, remember that onStop() might well be called on the way to onDestroy() - if the later is even called before destruction at all (there's no guarantee that it will be). So your timer delayed code might well not even get to run.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • I hope this is not accurate. Need to research how long is appropriate - what is Android's definition of a reasonable amount of time. Note that if there is any UI call where Android *should* be somewhat lenient, it is `onStop`, since that is an app's last chance to be sure they have preserved whatever they care about. Do you have any evidence as to what is/is not possible in onStop? – ToolmakerSteve Sep 22 '16 at 13:05
  • Allowable time has varied throughout the history and configuration of Android, but it has always been absolutely the rule that UI thread methods must promptly return. – Chris Stratton Sep 22 '16 at 14:08
-1

This is just a guess and completely untried but could you not call Thread.sleep(1000) before super.onStop()?

TMH
  • 6,096
  • 7
  • 51
  • 88