question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Rotation & Lifecycle: OnCreate or OnResume?

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://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/handling-rotation
https://docs.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.

dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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.

Do you request the permissions at runtime in the Activity? When calling the CheckSelfPermission command, the system will leave the current Activity which will trigger the
OnPause OnResume method. To avoid this, you could create a bool property to detect if the permission has been checked before.

public class MainActivity : AppCompatActivity
{
    bool IsChecked;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        ...
        if (!IsChecked)
        {
            if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == (int)Permission.Granted)
            {
                ...
            }
            IsChecked = true;
        }
    }
}
0 Votes 0 ·

I'm not sure what you're talking about. My question has nothing to do with permissions or external storage. I am simply unsure whether to put my code in OnCreate or OnResume, since in many (although not all) cases they are both called. Putting code in both would mean it would sometimes be called twice, but it obviously needs to be in at least one. Under what conditions are each one guaranteed to be called, and what actions are generally performed in each one?

0 Votes 0 ·

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

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.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.