进入应用前台调用特定代码
Call specific code on entering app foreground
我正在尝试将以下代码从 xamarin.iOS 移植到 xamarin.android:
public HomeView() : base(nameof(HomeView), null) {
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.WillEnterForegroundNotification, (NSNotification not) => {
if(NavigationController.TopViewController is HomeView)
ScrollToCurrentDay();
});
DispatchQueue.MainQueue.DispatchAsync(() => {
ScrollToCurrentDay();
});
}
当视图首次加载或应用程序进入前台时,如何调用函数?我尝试使用 BroadcastReceiver 参见 here。有没有更简单更快捷的方法?
Call specific code on entering app foreground
正如@MilanG 所说,Activity
的 OnResume() 方法将在您的应用程序从后台转到前台时被调用:
protected override void OnResume()
{
base.OnResume();
ScrollToCurrentDay();
}
如文档所述:
When the activity enters the Started state, the system invokes this callback. The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive.
然而,当Activity
是open by another Activity
时,Activity
的OnResume()
方法也会被调用。
没有任何直接的方法可以在后台或前台获取应用程序状态,您需要在Activity的生命周期中添加一个监听器。
解决方案一:
您可以创建一个 BaseActivity
来反映 Activity 生命周期回调,其他 Activity
必须扩展此 BaseActivity
.然后您可以跟踪您的活动在其生命周期中的时间并做出相应的反应。有关更多详细信息,您可以阅读此 document。这是一个例子:
public abstract class BaseActivity : AppCompatActivity
{
public static bool isAppWentToBg = false;
public static bool isWindowFocused = false;
public static bool isMenuOpened = false;
public static bool isBackPressed = false;
protected override void OnStart()
{
applicationWillEnterForeground();
base.OnStart();
}
private void applicationWillEnterForeground()
{
if (isAppWentToBg)
{
isAppWentToBg = false;
Toast.MakeText(ApplicationContext, "App is in foreground", ToastLength.Short).Show();
}
}
protected override void OnStop()
{
base.OnStop();
applicationdidenterbackground();
}
public void applicationdidenterbackground()
{
if (!isWindowFocused)
{
isAppWentToBg = true;
Toast.MakeText(ApplicationContext, "App is Going to Background", ToastLength.Short).Show();
}
}
//Called when the current Window of the activity gains or loses focus.
public override void OnWindowFocusChanged(bool hasFocus)
{
isWindowFocused = hasFocus;
if (isBackPressed && !hasFocus)
{
isBackPressed = false;
isWindowFocused = true;
}
base.OnWindowFocusChanged(hasFocus);
}
public override void OnBackPressed()
{
base.OnBackPressed();
if (this.GetType() == typeof(MainActivity)) {
} else {
isBackPressed = true;
}
}
}
方案二:
你可以注册一个ActivityLifecycleCallbacks来镜像所有的Activity生命周期回调,因为你使用MvvmCross
,建议使用这个解决方案:
[Application]
public class MyApplication : Application
{
public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
{
}
public override void OnCreate()
{
base.OnCreate();
RegisterActivityLifecycleCallbacks(new AppLifecycleListener());
}
}
public class AppLifecycleListener : Java.Lang.Object, Application.IActivityLifecycleCallbacks
{
private int numStarted = 0;
...
public void OnActivityStarted(Activity activity)
{
if (numStarted == 0)
{
// app went to foreground
Toast.MakeText(Android.App.Application.Context, "App is in foreground", ToastLength.Short).Show();
}
numStarted++;
}
public void OnActivityStopped(Activity activity)
{
numStarted--;
if (numStarted == 0)
{
// app went to background
Toast.MakeText(Android.App.Application.Context, "App is Going to Background", ToastLength.Short).Show();
}
}
}
我正在尝试将以下代码从 xamarin.iOS 移植到 xamarin.android:
public HomeView() : base(nameof(HomeView), null) {
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.WillEnterForegroundNotification, (NSNotification not) => {
if(NavigationController.TopViewController is HomeView)
ScrollToCurrentDay();
});
DispatchQueue.MainQueue.DispatchAsync(() => {
ScrollToCurrentDay();
});
}
当视图首次加载或应用程序进入前台时,如何调用函数?我尝试使用 BroadcastReceiver 参见 here。有没有更简单更快捷的方法?
Call specific code on entering app foreground
正如@MilanG 所说,Activity
的 OnResume() 方法将在您的应用程序从后台转到前台时被调用:
protected override void OnResume()
{
base.OnResume();
ScrollToCurrentDay();
}
如文档所述:
When the activity enters the Started state, the system invokes this callback. The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive.
然而,当Activity
是open by another Activity
时,Activity
的OnResume()
方法也会被调用。
没有任何直接的方法可以在后台或前台获取应用程序状态,您需要在Activity的生命周期中添加一个监听器。
解决方案一:
您可以创建一个 BaseActivity
来反映 Activity 生命周期回调,其他 Activity
必须扩展此 BaseActivity
.然后您可以跟踪您的活动在其生命周期中的时间并做出相应的反应。有关更多详细信息,您可以阅读此 document。这是一个例子:
public abstract class BaseActivity : AppCompatActivity
{
public static bool isAppWentToBg = false;
public static bool isWindowFocused = false;
public static bool isMenuOpened = false;
public static bool isBackPressed = false;
protected override void OnStart()
{
applicationWillEnterForeground();
base.OnStart();
}
private void applicationWillEnterForeground()
{
if (isAppWentToBg)
{
isAppWentToBg = false;
Toast.MakeText(ApplicationContext, "App is in foreground", ToastLength.Short).Show();
}
}
protected override void OnStop()
{
base.OnStop();
applicationdidenterbackground();
}
public void applicationdidenterbackground()
{
if (!isWindowFocused)
{
isAppWentToBg = true;
Toast.MakeText(ApplicationContext, "App is Going to Background", ToastLength.Short).Show();
}
}
//Called when the current Window of the activity gains or loses focus.
public override void OnWindowFocusChanged(bool hasFocus)
{
isWindowFocused = hasFocus;
if (isBackPressed && !hasFocus)
{
isBackPressed = false;
isWindowFocused = true;
}
base.OnWindowFocusChanged(hasFocus);
}
public override void OnBackPressed()
{
base.OnBackPressed();
if (this.GetType() == typeof(MainActivity)) {
} else {
isBackPressed = true;
}
}
}
方案二:
你可以注册一个ActivityLifecycleCallbacks来镜像所有的Activity生命周期回调,因为你使用MvvmCross
,建议使用这个解决方案:
[Application]
public class MyApplication : Application
{
public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
{
}
public override void OnCreate()
{
base.OnCreate();
RegisterActivityLifecycleCallbacks(new AppLifecycleListener());
}
}
public class AppLifecycleListener : Java.Lang.Object, Application.IActivityLifecycleCallbacks
{
private int numStarted = 0;
...
public void OnActivityStarted(Activity activity)
{
if (numStarted == 0)
{
// app went to foreground
Toast.MakeText(Android.App.Application.Context, "App is in foreground", ToastLength.Short).Show();
}
numStarted++;
}
public void OnActivityStopped(Activity activity)
{
numStarted--;
if (numStarted == 0)
{
// app went to background
Toast.MakeText(Android.App.Application.Context, "App is Going to Background", ToastLength.Short).Show();
}
}
}