OGeek|极客世界-中国程序员成长平台

标题: android - 在进入应用程序前台时调用特定代码 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:04
标题: android - 在进入应用程序前台时调用特定代码

我正在尝试将以下代码从 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 .有没有更简单更短的方法?



Best Answer-推荐答案


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.

然而,ActivityOnResume() 方法也会在Activityopen by another Activity 时被调用。 .

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

解决方案 1:

您可以创建一个 BaseActivity 来镜像 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4