2

We have Splash extends Activity which is the starting activity of our application. We also have CustomApplication extends Application class which is invoked when the app process in invoked.

Now we have the following requirement. Whenever the app/process is launched, call Utils.doSomeDBWork() function.

For this purpose we have put this function call in onCreate() of Splash and CustomApplication classes. The reason that we have put this call inside CustomApplication is that our application can be launched via deeplinks/notifications in which Splash won't be called. But the problem is that if the app was killed and launched via Splash, then the same function will be called twice. One from CustomApplication and the other through Splash.

So basically my question is that if the function has already been called from CustomApplication, then don't call this function from Splash. I can think of doing it by using some static variable or Shared Preferences. But don't think that this is a clean way. Is there any other way to achieve this, like passing some info through Intents etc?

Onik
  • 19,396
  • 14
  • 68
  • 91
thedarkpassenger
  • 7,158
  • 3
  • 37
  • 61

3 Answers3

2

How to know that Application class was called before Activity launch?

In brief, every time when Android "gets a request" to start any of your app component (Activity, Service, BroadcastReceiver) and your app isn't running yet, it forks the app_process (a.k.a zygote), changes its name to your.package.name defined in AndroidManifest.xml, initializes an Application instance, calls its onCreate() method, then instantiates the component requested and calls its lifecycle methods (Activity's onCreate(), Service's onCreate() or BroadcastReceiver's onReceive()).

For this purpose we have put this function call in onCreate() of splash and CustomApplication classes.

It's redundant. It's enough to call it only from Application's onCreate() which is the earliest "entry point" to an app that is guaranteed to be called before any other component's lifecycle methods. There can be only single instance of Application class which lives untill the app process dies.

You can easily test it by logging each of the lifecycle methods. After that you won't have any doubts left.

EDIT w.r.t the OP's comment:

If the app process was running and the user back presses and exits, and then launches the app again, then CustomApplication class won't be called.

This is only partially true. CustomApplication's onCreate() won't be called unless the system kills the app process while it is in background (simulate the case by, for example, swiping your app from Recents).

But our requirement is that CustomApplication class should be invoked in this case.

It's out of developer's scope. Only the system controls that.

That being said, CustomApplication's onCreate() will be called if Android kills the app in background. If it doesn't, a simple way to achieve the requirement is to have a boolean flag in CustomApplication which would indicate if Utils.doSomeDBWork() was called.

Onik
  • 19,396
  • 14
  • 68
  • 91
  • If the app process was running and the user back presses and exits, and then launches the app again, then CustomApplication class won't be called. But our requirement is that CustomApplication class should be invoked in this case. That's why we have put the that in onCreate() method of Splash class. – thedarkpassenger Oct 03 '17 at 09:31
0

Why don't you call from only applcation's class onCreate? when App is launched from deeplinks/notifications or Splash, Applcation will always be created first.

Also you can check from static variable. like below.

class Utils{
    public static boolean doneWork = false; // this static variable will be false when app process is killed.

    public doSomeDBWork(){
          if(!doneWork){
              //alreay done..
              return;
          }
          :
          :
          doneWork = true; 
    }

}
Soo Chun Jung
  • 595
  • 3
  • 14
0

You could do some controls with ActivityLifeCycleCallbacks on application layer. If using it in splashActivity is a solution for you,track your activities and make call in your callback. I hope it gives an idea.

atasoyh
  • 3,045
  • 6
  • 31
  • 57