繁体   English   中英

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

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

我有一个非常基本的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();
            }
        }
    });
}

此启动画面打开我的MainActivity 当我关闭此活动时,这是最近的应用程序中的屏幕截图:

在此输入图像描述

好像它制作截图太晚了,导致这个嵌套的最近应用截图。 这也发生在实际设备上。 我怎样才能解决这种奇怪的行为?

活动清单:

<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>

问题似乎不在launchMode 我一直有同样的行为。 即使拆除了lauchmode。

它与回调绝对有关。 直接启动活动时,没有问题。

编辑:

我找到了一个解决方案。 使用延迟启动活动会解决它。 虽然硬编码延迟不是很好。

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

编辑2

直接启动活动时我没有这种行为。 所以它可能与回调有关。 我正在使用谷歌登录。 看起来有些透明活动正在关闭。 这可能是Google登录活动吗?

而不是调用finish(); 你可以在你的应用程序清单中使用android:noHistory="true" ,如下所示:

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

或者您可以在Intent中使用FLAG_ACTIVITY_CLEAR_TOP

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

编辑:

为什么你使用android:launchMode="singleTask" 您应该将其更改为android:launchMode="singleInstance"或完全删除此行。 有关更多信息, 这里有一篇关于启动模式的精彩文章

我猜你的问题是由于使用没有taskAffinity launchMode singleTasktaskAffinity

让我们改变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>

希望它有所帮助!

我无法重现您所描述的问题,但由于您说使用Handler.postDelayed()帮助,请尝试下面的代码。

通过在onResume()/onPause()之间添加/删除侦听器,您还可以避免在应用程序处于后台时启动活动。 此外,如果FirebaseAuth包含一个侦听器列表,那么如果多次触及onCreate()方法,它将保留对已经销毁的活动的引用,例如在旋转时。 所以你可以避免内存泄漏。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM