Track count of app launch in android library for analytics - android

I am developing an Android library for analytics. I have created a method to get all device data. Application class of the app will call this library method.
I want to track app launch count also. I tried to use the same method but Application class does not get called every time. For first app launch, got call in application class but when I tried to exit app using back press and launch again, I did not get any call.
Last option for me to track launch count is ask application to add one more call in on-create of launcher activity but is there any better way? How firebase and other analytic libraries implements this?

When you close your app by doing back press again and again. It pushes the application in background but doesn't kill it. So the application class is not initiated again. So this is not treated as App Launch.
App Launch is triggered when the user kills the app or the OS kills the app in background due to limitation of resources.
So, what you have done is fine...but if you want to beyond this you would need to implement your own logic like you mentioned (using onCreate of activity)

Related

Is there a way to know the reason the app was launched?

In my custom Application class, I want to know the reason that the app was launched. For example, was it launched because of a remote notification, the user launched the app directly, etc.
On iOS, When the application launches I can use the LaunchOptionsKey in the application(_:didFinishLaunchingWithOptions:) method in order to know the reason the app was launched.
launchOptions.
A dictionary indicating the reason the app was launched (if any).
Is there any equivalent on Android?

Splash screen re-showing when launched from 3rd party app

I have implemented a splash screen for my app. I made the splash screen activity the launch activity. The splash activity does the startup work (loading data, etc.), and then launches the "real" main activity.
The problem is this: I am using a 3rd party app that has the ability to launch other apps. Let's say my app is already running (it is past the splash screen, and is on to the real main screen). I then press the Home button to put the app in the background. I then do something that causes the 3rd party app to launch my app. What I want is for my app to be simply restored (as it would if I had tapped the app icon from the phone's regular launcher). Instead, it launches the splash screen all over again, and my initialization code runs again, which I don't want.
Since this is a 3rd party app that is starting my app, I don't have the ability to change how it launches my app. I am assuming that it is just querying the package manager to get the launch intent and starting that. So, I need to find a way to fix this in my app itself.
Is there a way that I can find out from my splash screen's onCreate method if my "real" main activity is already running, and if so, skip the initialization code and bring the existing main activity to the foreground?
Keep a static boolean in your Application class.
When initialization is done, set it to true.
When splash screen starts, check the value of the boolean, if true, go straight to your main activity without doing any logic, and instantly finish the splash activity.
If false, then assume it's a cold start and you need to run the initialization code.
There is no way to look at the Activity stack from within an Android app, so you can't check if the Main activity is already running.
There is also no way for the 3rd party app to check if your app is running, and then launch a different activity depending on that.

How to prevent app restart when using launcher after store?

My Android application is being restarted when using the launcher to launch it after I have used the app store to launch it (and visa versa). Is there any way to prevent this?
By restarted I mean that the activity stack is lost. This is important as our users are setting up and returning to an activity in the app intermittently over the course of an hour or so. After first install, they will likely have installed and opened the app from the app store, set themselves up and then backgrounded the app. Later they are likely to open the app from the launcher and lose all their state!
The problem is further compounded as we start a foreground service along with the set-up activity. Clicking the services notification should bring the user back to the set-up activity but, as with the launcher, if the user originally opened the app from the Play store, they again lose all their state!
Reproducing the problem
I've made a sample application here:
https://github.com/samskiter/LaunchTest
Note: it uses the BBC weather application package Id in order to allow you to quickly open from the app store (the "Open" button will be shown on the BBC weather application if this app is installed).
Steps are as follows:
Uninstall the BBC weather app if you have it
Install the LaunchTest app
Close the LaunchTest app from recents
Open the LaunchTest app from the BBC weather app page on the Play Store
Click the button to navigate to the Second activity
Background the application (press home)
Open the LaunchTest application from the app launcher
The state is lost! you are back at the First (root) activity
What I've tried
Using singleTask launch mode hasn't helped - it causes the app to be relaunched even if you use the launcher every time.
I've tried alwaysRetainTaskState - I don't really expect this to work as this only really affects things over about a 30 minute wait.
What I think is going on
There is no mechanism in the activity manager / intent system to open a running app in it's current state. Instead, I think the UID of the launching application is taken into account. If it is different then the Intent.FLAG_ACTIVITY_NEW_TASK flag is forced and so created a new task and dropping all my users' lovely state.
Inspecting Google Maps
Google maps has a very similar interaction model to our application: a setup UI, followed by an ongoing process the user is going through for a long period of time (navigation) with a paired, foreground service (the navigation service you can see in your notification bar). BUT GMaps doesn't suffer from this problem. I think this is because it uses only a single activity for all of it's interface and uses singleTask. So now, when you tap on the launcher after originally launching from the play store, the task can be reused.
In my opinion this exposes a hole in the android intent/activity management system. The whole point of the savedInstanceState/activity lifecycle is to prevent dropping state, but here we have a way to dump everything. My current best solution is to detect app restarts by the fact the service is running and try to get the user back to where they were, which is more than a little tricky.
If someone knows a way I can prevent my state being dropped on the floor when reopening from the app launcher after opening from the store, I would really appreciate it.
This is more like a workaround to your problem, but it seems that there may be no real solution.
My first thought was - since the whole setup will take a while anyway - why don't you just save some kind of bookmark ('firstLaunch') to Preferences, post a (delayed?) notification and finish the app, so that the user has to open it again by tapping on the launcher icon. Only then you start the real setup, so you will not lose information due to the installer vs launcher problem.
But the problem seems to have been around for some time and the following SO-posts may help:
Re-launch of Activity on Home button, but…only the first time
After tap on app icon,launcher create a new instance of root activity again & again
Hope this helps!

How to implement Android BroadcastReceiver

I created a sample application. In my application I have 4 Activitys and 1 BroadCastReciever that runs in background. Here I wrote there service for the purpose of when the user Unlock the screen, I run my application. Everything works fine except this:
When I run my application, I'm in my third activity. The user no longer interacts with screen so the device gets auto screen locked. Now I unlock the screen means, the application runs from the first activity. I need to run the application from the user's last interacted activity (for example, if the user interacted with the third activity, I need to show the third activity when the screen unlocks).
No problem, use SharedPreferences and store a currentActivityName. Update this when you enter a new Activity via onCreate or onStart(). This value will be preserved, even when the app shuts down. Once you detect your entry point you would just startActivity(activityName) once the app returns.
Do not finish the activity when you are moving from one activity to another activity and add WAKE_LOCK permissions in android manifest file

Launch application without an interface to receive boot_completed

I have developed a Service for Android that starts when BOOT_COMPLETED is received.It works fine on versions prior to 3.1.
I newer versions tough I understand the application must be launched in order to receive this broadcast . How can I launch my application if I don't have an User Interface ?
You add a user interface, in the form of an activity that the user can launch. Besides, this is where you need to:
Allow the user to stop and start your service, since the user may not want your service to be running 24x7 and you do not want the user to have to resort to Force Close
Display your documentation
Display your license agreement

Categories

Resources