Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Fixes #309, update to set activity on init and oncreate, never null. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno authored and Redth committed Jun 25, 2018
1 parent 7d41c50 commit 76f2b1d
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions Xamarin.Essentials/Platform/Platform.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ public static void Init(Application application)
application.RegisterActivityLifecycleCallbacks(lifecycleListener);
}

public static void Init(Activity activity, Bundle bundle) =>
Init(activity.Application);
public static void Init(Activity activity, Bundle bundle)
{
Init(activity.Application);
lifecycleListener.Activity = activity;
}

public static void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) =>
Permissions.OnRequestPermissionsResult(requestCode, permissions, grantResults);
Expand Down Expand Up @@ -110,26 +113,24 @@ class ActivityLifecycleContextListener : Java.Lang.Object, Application.IActivity
internal Context Context =>
Activity ?? Application.Context;

internal Activity Activity =>
currentActivity.TryGetTarget(out var a) ? a : null;

void Application.IActivityLifecycleCallbacks.OnActivityCreated(Activity activity, Bundle savedInstanceState)
internal Activity Activity
{
get => currentActivity.TryGetTarget(out var a) ? a : null;
set => currentActivity.SetTarget(value);
}

void Application.IActivityLifecycleCallbacks.OnActivityCreated(Activity activity, Bundle savedInstanceState) =>
Activity = activity;

void Application.IActivityLifecycleCallbacks.OnActivityDestroyed(Activity activity)
{
}

void Application.IActivityLifecycleCallbacks.OnActivityPaused(Activity activity)
{
currentActivity.SetTarget(null);
}
void Application.IActivityLifecycleCallbacks.OnActivityPaused(Activity activity) =>
Activity = activity;

void Application.IActivityLifecycleCallbacks.OnActivityResumed(Activity activity)
{
currentActivity.SetTarget(activity);
}
void Application.IActivityLifecycleCallbacks.OnActivityResumed(Activity activity) =>
Activity = activity;

void Application.IActivityLifecycleCallbacks.OnActivitySaveInstanceState(Activity activity, Bundle outState)
{
Expand Down

0 comments on commit 76f2b1d

Please sign in to comment.