[英]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 singleTask
而taskAffinity
。
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.