Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Unit test for app life cycle - is it possible?

ZenDroidZenDroid USMember ✭✭✭

I use Xamarin Forms for my app on Android. I want test the app after event OnSleep and OnResume with unit test. Is it posible?


  • JohnHardmanJohnHardman GBUniversity admin

    @FetFrumos911 - Using Xamarin UITest to test lifecycle events requires lateral thinking. It's a while since I used UITest, but I definitely had this working.

    As a reminder, here's what the documentation says at for the different events:

    OnStart - Called when the application starts.
    OnSleep - Called each time the application goes to the background.
    OnResume - Called when the application is resumed, after being sent to the background.

    If I remember correctly (I haven't got the code in front of me right now), when my app was in background, I think calling StartApp again resulted in OnResume being fired. Of course, your app may be terminated when it is in background, so you might want to mutate your app code slightly so that you can check whether OnStart or OnResume is called and try again as required.

    To get the app to background, simply bring another app to the foreground. My app has the ability to open a page in an external browser by tapping on a hyperlink. To get OnSleep to fire, I simply tapped on that hyperlink. If you app doesn't have anything similar, you could add a backdoor for testing purposes (see )

  • ZenDroidZenDroid USMember ✭✭✭

    thank you very much. you have shown the way.

  • ZenDroidZenDroid USMember ✭✭✭

    I use backdoor. I opened link in browser. I need return to app. Is it possible?

  • JohnHardmanJohnHardman GBUniversity admin

    @ZenDroid - Yes - call StartApp again. using AppDataMode.DoNotClear . If I remember correctly, StartApp will bring a backgrounded app back to the foreground. I don't think it's documented, but my memory tells me that's what I found.

  • ZenDroidZenDroid USMember ✭✭✭

    Thank you. It is work.

Sign In or Register to comment.