1

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

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98

3 Answers3

1

One way is to call finish(); in your activity onStop(). This way your app will re-open the launcher activity next time.

Abdullah Riaz
  • 536
  • 5
  • 19
1

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.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

I think what you want is not to open a splash screen every time, but to show some specific screen on app resume. Ideologically splash screen shouldn't be shown every time user returns to the app.

The approach you'r using for that is correct. If you want a screen that will show your image instead of white screen, you should implement it via Theme.

  1. Create a background resource that will contain your image.
  2. Specify this resource as android:windowBackground in you "splash" activity theme.
  3. Set these theme to be used in your "splash" activity.

For more info on that check this article.

Demigod
  • 5,073
  • 3
  • 31
  • 49