gpt4 book ai didi

android - 进入应用前台调用特定代码

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:00 24 4
gpt4 key购买 nike

我正在尝试将以下代码从 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();
});
}

当 View 首次加载或应用程序进入前台时,您如何调用函数?我尝试使用 BroadcastReceiver 参见 here .有没有更简单、更短的方法?

最佳答案

Call specific code on entering app foreground

正如@MilanG 所说,ActivityOnResume()当您的应用程序从后台进入前台时将调用该方法:

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.

但是,当此Activityopen by another Activity 时,ActivityOnResume() 方法也会被调用。 .

没有任何直接的方法可以在后台或前台获取应用程序状态,您需要在 Activity 的生命周期中添加一个监听器。

解决方案 1:

您可以创建一个 BaseActivity 来反射(reflect) Activity 生命周期回调,所有其他 Activity 都必须扩展此 BaseActivity。然后您可以跟踪您的 Activity 在其生命周期中的时间并做出相应的 react 。有关更多详细信息,您可以阅读此 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;
}

}
}

解决方案 2:

你可以注册一个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();
}
}
}

关于android - 进入应用前台调用特定代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46888716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com