Intercept creation of activities when the application is restored
        Posted  
        
            by 
                Johan Bilien
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Johan Bilien
        
        
        
        Published on 2013-10-28T21:51:28Z
        Indexed on 
            2013/10/28
            21:53 UTC
        
        
        Read the original article
        Hit count: 233
        
Most of our activities access a user-specific model. All these activities inherit from a ModelActivity base class, which provides a getModel() call. When one of these activities detect that the user has signed out (through the AccountManager callback), it sticks to its existing model, but prepares to exit back to the root activity (which is not user-specific) by starting its intent with FLAG_ACTIVITY_CLEAR_TOP.
If however the user deletes an account while the app is not running, we run into trouble when the activity is restored. Now the activity needs to handle there not being a model, which makes the code more complicated and bug-prone.
Ideally we would intercept the application restore process before the activity is created. Then we would check whether we have an account and a model, and if not clear up the saved stack of activities, and restart from our root activity instead of the last saved activity. But as far as I can tell the first place where we can run code is in the onCreate callback of the activity.
Is there a way to run some code when the application is restored from background-saving, but before the saved activity is created?
© Stack Overflow or respective owner