I got a very basic splashscreen activity:
@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
. 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
. I keep having the same behaviour. Even when removing the 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
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?
Instead of calling finish();
you can use the android:noHistory="true"
in your app manifest like this:
<activity
android:name=".SplashActivity"
android:noHistory="true" />
Or you can use FLAG_ACTIVITY_CLEAR_TOP
in your Intent:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Edit:
Why do you use the android:launchMode="singleTask"
? You should probably change this to android:launchMode="singleInstance"
or remove this line completely. For more information, here is a great article about launch modes
I guess that your problem occur due to using launchMode singleTask
without taskAffinity
.
Let's change the 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.
By adding/removing the listener between onResume()/onPause()
, you also avoid starting an activity when your app is in the background. 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. 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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.