4

I see a lot of answers concerning the Activity Lifecycle and that for most part is clear to me. What I'm looking for is the Application Lifecycle. I have the following case:

  1. Application starts - Invalidate PIN
  2. A PIN has to be entered if it is stale of invalid
  3. Application is backgrounded (Android Home screen is visible) - Invalidate PIN
  4. The applicatiion is foregrounded (App becomes visible again) - goto step 2

With the Activity Lifecycle it is hard if not impossible to achieve. Any suggestions?

  • When you mean backgrounded, you mean that the application is no longer visible, right? – horro Jun 06 '17 at 10:39
  • Right, that is what I mean – Paul Sinnema Jun 06 '17 at 10:40
  • Hard to undrstand what exactly are you looking for, but I would suggest looking at Android Services: https://developer.android.com/guide/components/services.html – Vaiden Jun 06 '17 at 10:42
  • Which part do you not understand. Is the description not clear enough? – Paul Sinnema Jun 06 '17 at 10:43
  • 1
    Ye sorry. Look at this Q&A. Specifically the most upvoted answer. (not the accepted one) https://stackoverflow.com/a/15573121/940834 – IAmGroot Jun 06 '17 at 10:44
  • The problem is that activity code is called over and over again for each activity becoming active of being stopped. As long as the Application is active there is no need to enter a new PIN but when the App is pushed to the background and later resumed I want the user to enter a PIN again for security reasons. – Paul Sinnema Jun 06 '17 at 10:52
  • Ah, I should have mentioned I'm using Xaramin with C# and Visual Studio – Paul Sinnema Jun 06 '17 at 10:53
  • The Application itself doesn't have much of a lifecycle. It is created then killed. You need to monitor all Activities in order to determine if one of them is visible at a given time. You can make them all inherit from a base Activity in order to make it easier. – BladeCoder Jun 06 '17 at 11:04
  • @Doomsknight: It is a dirty hack. Is there no cleaner way to do this? – Paul Sinnema Jun 06 '17 at 11:27
  • Another interesting method here: https://stackoverflow.com/a/29999738/940834 Which may be seen as cleaner.. not sure – IAmGroot Jun 06 '17 at 11:34

3 Answers3

1

Inspired by the solution Doomsknight pointed me to I constructed this solution without the Timer as proposed in the proposed answer. Here's the code from my mainapplication.cs:

    public void OnActivityPaused(Activity activity)
    {
        _lastActivity = DateTime.Now;
    }

    public void OnActivityResumed(Activity activity)
    {
        CrossCurrentActivity.Current.Activity = activity;

        DateTime now = DateTime.Now;
        TimeSpan span = now - _lastActivity;

        if (span.TotalMilliseconds > 2000)
        {
            Notifier.Classes.Settings.IsPinValid = false;
        }

        _lastActivity = now;
    }
0

When your application is backgrounded (no longer visible), the method called is onStop(), and when the application is foregrounded, onStart() and then onResume() will be called (onCreate() could be called as well).

The thing is, what you should do is invalidate PIN in onStop(), and then ask for the PIN in onResume(). This will always make your application ask for the PIN: the first time the application starts and whenever it is backgrounded.

horro
  • 1,262
  • 3
  • 20
  • 37
0
  1. Application starts - onCreate()/ onStart()

  2. Get the pin in onCreate or onStart or onResume() (check the state of the pin.

3.Application is backgrounded - onPause() - Invalidate your pin on onPause() method

4.The applicatiion is foregrounded - onResume() - perform step 2 in onResume()

Ezio
  • 2,837
  • 2
  • 29
  • 45
  • I would not invalidate the PIN in `onPause()`, because the application could be partially visible, right? – horro Jun 06 '17 at 10:51
  • 1
    It doesn't matter because you are going to validate again in onResume() – Ezio Jun 06 '17 at 10:53
  • Maybe I'm missing the point here. The OnStart() and OnStop() are called for every activity. It does not say anything about the Application being push/popped from the background. – Paul Sinnema Jun 06 '17 at 11:00
  • Could this have something to do with MvvmCross? I use the ShowViewModel() method to show a new view. – Paul Sinnema Jun 06 '17 at 11:02