1

I use OneSignal to send push notifications in my Android app. Here is my onCreate() method of Application class :

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        OneSignal.startInit(this)
                .setNotificationOpenedHandler(new MyNotificationOpenedHelper())
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .init();

        Log.i("onesignaal", "app onCreate()");
    }
}

But the problem is here: When I run the app first time, onCreate() method called as expected. But when I press home button and clear my app from working app, the onCreate() method recalled. And whenever I run the app after first run, onCreate() method does not called when app launches, it called everytime I completely close the app. (everytime I completely close the app and then run)

When I delete OneSignal initialization :

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();        

        Log.i("onesignaal", "app onCreate()");
    }
}

onCreate() method runs as aspected : it called everytime app launches and does not called closing app completely. Where is the problem?

EDIT :

I found the reason. I have a method to get OneSignal user id and save it my database. When I call that method on onCreate() method of Application, the problem occurs. But when i do not call that method, onCreate() method works as aspected. Here that function :

private void saveOnesignalId(){

    OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
        @Override
        public void idsAvailable(final String userId, String registrationId) {

            Log.i("onesignaal", "userId : " + userId);
            Log.i("onesignaal", "registrationId : " + registrationId);

        }
    });

}

But i still need solution.

EDIT : Here all of the Application class:

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        SugarContext.init(this);

        OneSignal.startInit(this)
                .setNotificationOpenedHandler(new MyNotificationOpenedHelper())
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .init();

        Log.i("onesignaal", "app onCreate()");

        saveOnesignalId();

    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        SugarContext.terminate();
    }


    private void saveOnesignalId(){

        OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
            @Override
            public void idsAvailable(final String userId, String registrationId) {

                Log.i("onesignaal", "userId : " + userId);
                Log.i("onesignaal", "registrationId : " + registrationId);

            }
        });

    }


}

EDIT : I think the problem is on OneSignal SDK. When I delete all OneSignal methods, there is no problem. onCreate() method works properly. But when I any of OneSignal methods, the onCreate() method of the applications do not work properly.

cimenmus
  • 1,386
  • 4
  • 18
  • 31
  • ok, 1) Does your onCreate gets called on rerun of the app? - that means clearing your apps from the recently open apps(Working apps) 2) Does your onCreate gets called on rerun of the app? - that means NOT clearing your app from recently open apps(working apps) – Smit Sep 03 '16 at 12:57
  • I clear my app from recently open (woking apps). But when I rerun the app, onCreate() method of the App class does not called, it called after I clear my app from working apps – cimenmus Sep 03 '16 at 13:39

2 Answers2

2

Application.onCreate is called when your app's process is started just before any Activities, BroadcastReceivers , or Services are started. You will need to consider this when placing any code in the method as it is not always related directly to a user interaction with your app.

The reason you are seeing onCreate being fired when the app is swiped away after you added OneSignal is it adds a SyncService Service class. It is started in a sticky state with your app to ensure any pending tags and other session fields are sent. When your app is swiped away Android kills your process, then starts it up again with OneSignal's SyncService. The service will check for any pending changes and will stop it self if there is not.

Any Intent sent to your app (like a received GCM message) will also cause onCreate to be called. For more details on the Application and process lifecycle (different from the Activity lifecycle) I recommend reading the following.

https://developer.android.com/guide/topics/processes/process-lifecycle.html https://developer.android.com/reference/android/app/Application.html#onCreate() https://developer.android.com/guide/components/processes-and-threads.html

jkasten
  • 3,899
  • 3
  • 32
  • 51
0

I presume by

when I press home button and clear my app from working app

you mean that you app is running on the background.

See the thing is this is the default android lifecycle behavior. When you don't kill the app explicitly i.e remove it from the background the app is not destroyed. And goes into pause mode i.e onPause method is called and when you re open the app onResume is called since the app is not destroyed yet there is no reason to recreate the app. As shown in the below diagram

enter image description here

I strongly suggest you go through this and read/learn about the android lifecycle activity events and see which methods are called during different state of the activity.

Community
  • 1
  • 1
Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45
  • I edited the question. But when I do not call saveOnesignalId() method, whenever I run the app (after pressing home button and clearing from working apps) onCreate() method of Application class called. But when I call saveOnesignalId() method in onCreate(), onCreate() method does not called at app is launching, it called just after I clear my app from working apps. – cimenmus Sep 03 '16 at 13:56
  • Can you share your OneSignal class ? – Gaurav Sarma Sep 03 '16 at 14:06
  • No i need your OneSignal.java class – Gaurav Sarma Sep 03 '16 at 14:22
  • I do not have OneSignal.java class. I use last OneSignal SDK for Android. – cimenmus Sep 03 '16 at 14:25
  • Sorry i didn't read your comment properly above but what you have wrote there makes perfect sense regardless of you calling the saveOnesignalId() your onCreate won't be called if you don't remove the app from your background i.e. kill the app. And please don't write working app and use the correct term as app in background . – Gaurav Sarma Sep 03 '16 at 14:56
  • I think there may be a bug on OneSignal SDK. Because I deleted saveOnesignalId() method in OnCreate(), just called OneSignal.startInit(this).init(), but nothing changed. – cimenmus Sep 03 '16 at 15:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122578/discussion-between-gaurav4sarma-and-cimenmus). – Gaurav Sarma Sep 03 '16 at 15:28