Rotation & Lifecycle: OnCreate or OnResume?

Nathan Sokalski 4,111 Reputation points
2021-05-28T18:42:46.127+00:00

As we all know, it is important to maintain things like state & appearance while using an app. We also know that some of the most common scenarios in which this must be handled are rotation & lifecycle stages like sleep, closing the app, backgrounding, etc. I have looked at the following pages for help on these:
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/handling-rotation
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/activity-lifecycle/
From what I can tell, most of the code involved in making sure the app maintains a consistent appearance should go in OnCreate or OnResume. However, I am having trouble determining what code should go in which one. The diagram on the Activity Lifecycle page does not include what scenarios trigger each event, and whether the event is called always or only sometimes. I basically need a list of which events are raised (and whether they are raised always or only sometimes) for each of the following situations (maybe I forgot to list some here, but these are the ones I am most concerned about):

  1. Opening the app (I know that this triggers OnCreate & OnResume)
  2. Rotating an Activity
  3. Navigating to a new Activity in the same app
  4. Navigating to an Activity in the same that was navigated away from
  5. Returning to the app after being backgrounded or going to sleep

I think most of the involved code would be the same for OnCreate & OnResume, but I am not sure how to avoid having it executed multiple times. Are there any good tutorials or diagrams that include both the scenarios & methods/events, as well as which methods/events are always triggered vs which ones are only sometimes? Thanks.

Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,971 Reputation points
    2021-06-01T02:39:44.547+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I am simply unsure whether to put my code in OnCreate or OnResume, since in many (although not all) cases they are both called.

    OnResume event will be called every time the Activity comes to the foreground. OnCreate ** fires when the system creates the activity. When the screen is rotated, the Activity will be recreated and some lifecycle methods such as **OnCreate and OnResume will be called again. Check the doc.

    If you don't want to recrate the Activity, you can declare that your activity handles the configuration change itself which prevents the system from restarting your activity. Set the ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize configuration for the Activity as below.

       [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize)]  
       public class MainActivity : AppCompatActivity  
       {  
           ...  
       }  
    

    Related doc: https://developer.android.com/guide/topics/resources/runtime-changes#HandlingTheChange

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.