I want to open a screen which contains an image and when my application is resumed then I want to show that screen. But the problem is, it shows the white screen first when the application is resumed.
How to remove that white screen and open like WhatsApp screen every time?
Here is my App Class
class App : Application() {
private var context: Context? = null
override fun onCreate() {
super.onCreate()
context = this
registerActivityLifecycleCallbacks(AppLifecycleTracker())
}
companion object {
@SuppressLint("StaticFieldLeak")
private val instance: App? = null
fun getContext(): App? {
return instance
}
}
And the AppLifecycleTracker
class
class AppLifecycleTracker : Application.ActivityLifecycleCallbacks {
override fun onActivityPaused(activity: Activity) {
println("AppLifecycleTracker onActivityPaused")
}
override fun onActivityDestroyed(activity: Activity) {
println("AppLifecycleTracker onActivityDestroyed")
}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
println("AppLifecycleTracker onActivitySaveInstanceState")
}
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
println("AppLifecycleTracker onActivityCreated")
}
override fun onActivityResumed(activity: Activity) {
println("AppLifecycleTracker onActivityResumed ${activity}")
}
private var numStarted = 0
override fun onActivityStarted(activity: Activity?) {
if (numStarted == 0) {
println("AppLifecycleTracker Foreground")
if (App.sinltonPojo?.launchData == 1) {
activity?.startActivity(Intent(activity.application, GifViewActivity::class.java))
activity?.overridePendingTransition(R.anim.enter, R.anim.exit)
}
}
numStarted++
}
override fun onActivityStopped(activity: Activity?) {
numStarted--
if (numStarted == 0) {
// app went to background
}
}
}
There are many ways of doing this actually. You might consider the following.
Instead of registering for lifecycle callbacks from your Application
class, I think you might have a SplashActivity
as the launcher activity and use android:noHistory="true"
in your AndroidManifest.xml
for all other activities.
<activity
android:noHistory="true"
android:label="@string/app_name"
android:name=".activities.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:noHistory="true"
android:name=".activities.MainActivity" />
In this way, you can have your SplashActivity
launched each time it resumes from the background and you can have the logic of transitioning to other activities in your SplashActivity
.
I hope that helps.