简体   繁体   English

为什么在firebase onAuthStateChanged中完成活动时,最近的应用程序中没有屏幕截图

[英]Why is there no screenshot in recent apps when finnishing activity in firebase onAuthStateChanged

I got a very basic splashscreen activity: 我有一个非常基本的splashscreen活动:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);

    FirebaseAnalytics.getInstance(this);


    mAuth = FirebaseAuth.getInstance();
    mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                FirebaseAuth.getInstance().removeAuthStateListener(this);
                startActivity(new Intent(StartActivity.this, MainActivity.class));
                finish();
            }
        }
    });
}

This splash screen opens my MainActivity . 此启动画面打开我的MainActivity When I close this activity this is the screenshot in recent apps: 当我关闭此活动时,这是最近的应用程序中的屏幕截图:

在此输入图像描述

It seems like it is making a screenshot just too late, which results in this nested recent apps screenshot. 好像它制作截图太晚了,导致这个嵌套的最近应用截图。 This also happen on an actual device. 这也发生在实际设备上。 How can I solve this weird behaviour? 我怎样才能解决这种奇怪的行为?

Activies manifest: 活动清单:

<activity
    android:name="activities.StartActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity
    android:name="activities.MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:launchMode="singleTask">
</activity>

The problem seems not the be in the launchMode . 问题似乎不在launchMode I keep having the same behaviour. 我一直有同样的行为。 Even when removing the lauchmode. 即使拆除了lauchmode。

It has absolutely something to do with the callback. 它与回调绝对有关。 When starting the activity directly, there is no problem. 直接启动活动时,没有问题。

EDIT: 编辑:

I kind a found a solution. 我找到了一个解决方案。 Using a delay in starting the activity resolves it. 使用延迟启动活动会解决它。 Although a hardcoded delay isn;t that nice. 虽然硬编码延迟不是很好。

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        startActivity(new Intent(StartActivity.this, MainActivity.class));
        finish();
    }
},500);

EDIT 2 编辑2

I do not get this behaviour when directly starting the activity. 直接启动活动时我没有这种行为。 So it probably has something to do with the callback. 所以它可能与回调有关。 I'm using google sign-in. 我正在使用谷歌登录。 It looks like some transparent activity is being closed. 看起来有些透明活动正在关闭。 Could this be the Google Sign-in Activity? 这可能是Google登录活动吗?

Instead of calling finish(); 而不是调用finish(); you can use the android:noHistory="true" in your app manifest like this: 你可以在你的应用程序清单中使用android:noHistory="true" ,如下所示:

<activity
    android:name=".SplashActivity"
    android:noHistory="true" />

Or you can use FLAG_ACTIVITY_CLEAR_TOP in your Intent: 或者您可以在Intent中使用FLAG_ACTIVITY_CLEAR_TOP

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Edit: 编辑:

Why do you use the android:launchMode="singleTask" ? 为什么你使用android:launchMode="singleTask" You should probably change this to android:launchMode="singleInstance" or remove this line completely. 您应该将其更改为android:launchMode="singleInstance"或完全删除此行。 For more information, here is a great article about launch modes 有关更多信息, 这里有一篇关于启动模式的精彩文章

I guess that your problem occur due to using launchMode singleTask without taskAffinity . 我猜你的问题是由于使用没有taskAffinity launchMode singleTasktaskAffinity

Let's change the AndroidManifest.xml 让我们改变AndroidManifest.xml

<activity
    android:name="activities.StartActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity
    android:name="activities.MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:launchMode="singleTask"
    android:taskAffinity="your_any_string">
</activity>

hope it help! 希望它有所帮助!

I was not able to reproduce the issue you are describing, but since you say that using Handler.postDelayed() helps, try the code below. 我无法重现您所描述的问题,但由于您说使用Handler.postDelayed()帮助,请尝试下面的代码。

By adding/removing the listener between onResume()/onPause() , you also avoid starting an activity when your app is in the background. 通过在onResume()/onPause()之间添加/删除侦听器,您还可以避免在应用程序处于后台时启动活动。 Also, if the FirebaseAuth holds a list of listeners, it will keep references to activities that were already destroyed if hits the onCreate() method multiple times, for example on rotation. 此外,如果FirebaseAuth包含一个侦听器列表,那么如果多次触及onCreate()方法,它将保留对已经销毁的活动的引用,例如在旋转时。 So you can avoid memory leaks. 所以你可以避免内存泄漏。

private FirebaseAuth.AuthStateListener authStateListener;

@Override
protected void onResume() {
    super.onResume();

    FirebaseAnalytics.getInstance(this);

    authStateListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                FirebaseAuth.getInstance().removeAuthStateListener(this);
                authStateListener = null;
                startActivity(new Intent(StartActivity.this, MainActivity.class));
                finish();
            }
        }
    };
    FirebaseAuth.getInstance().addAuthStateListener(authStateListener);
}

@Override
protected void onPause() {
    super.onPause();
    if (authStateListener != null) {
        FirebaseAuth.getInstance().removeAuthStateListener(authStateListener);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么通过最近的应用程序恢复活动时 Mediaplayer 不工作? - Why does the Mediaplayer not work when the activity is resumed through the recent apps? Android什么时候拍摄其最近的应用切换器屏幕截图? - When does Android take its recent apps switcher screenshot? 从最近的应用程序恢复活动后,为什么要重新传达意图? - Why does an intent gets redelivered when returning to activity from recent apps? 当我的应用程序在“最近使用的应用程序”列表中时,如何启动特定活动? - How to Launch specific activity when my Application is in Recent Apps List? 当绑定活动被最近的应用程序杀死时,正确停止服务 - Properly stop a service when the binding activity is killed by recent apps 从窗口小部件启动时,活动未显示在最近的应用列表中 - Activity not showing in list of recent apps when launched from a widget 从“最近的应用程序”启动时,PopUp Activity再次启动 - PopUp Activity starts again when it is started from “recent apps” Android:自定义最近的应用程序缩略图(默认截图) - Android: Customizing recent apps thumbnail (screenshot by default) 为什么“活动屏幕”在Redmi设备中从后台服务启动活动时未显示最近的应用程序 - Why “Activity Screen” doesnot shown RECENT Apps while activity launch from background service in Redmi device 当我从最近的应用程序中删除应用程序时,为什么没有触发警报? - Why Alarm not fired when I remove app from recent apps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM