• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

android - 在进入应用程序前台时调用特定代码

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:04:18 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试将以下代码从 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap