multiple instances of gps activity is running at same time - android

I have got an app tracking GPS.
I am using myLocationOverlay to get location and I have got kind of navigation.
there are two options when closing app.
gps keeps tracking (not calling myLocationOverlay.disableMyLocation;)
gps is disabled.
when GPS keeps tracking it gives Notifications and when I click to the Notification, there is no problem, it opens the currently running activity with pending Intent.
But when I open the same activity again from the application, there appears a new instance of the same activity and it starts to use the GPS and updates the notification.
So two instances of same activity are working at the same time.
I tried to close the previous activity when second instance is opened but I couldn't do it.
I've also used intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); when opening new intent and also put this android:launchMode="singleInstance"
in AndroidManifest.xml but the first created one still works.
I hope someone has an idea.

Try using FLAG_ACTIVITY_CLEAR_TOP in conjunction with FLAG_ACTIVITY_NEW_TASK. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.

Related

Activity does not start when called from service when relaunching the app

I have a MainActivity which launches a background service. This service monitors the location of the device and if the device is in particular location, It launches another activity LocationBasedActivity which comes to the foreground plays a video, once complete it launches the MainActivity using startActivity.
This works well for the first time when the app is installed. But when the device is restarted and the app is launched, all logic of detecting the location works, but the LocationBasedActivity is not launched. The call to startActivity is being made, but it returns without any error or exception, the following is the code snippet for launching the LocationBasedActivity from the service
Intent intent = new Intent(MyBackgroundService.this, LocationBasesActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I tried debugging this, got to the point where the call to Android's tried debugging the startActivity which in turn is calling
This is on Android 4.4 on Tab E. Would really appreciate any help on this issue.
Thanks

Why App after crash lands on some activity and does not exit

I have noticed this dual behavior of app crash in different applications. Sometimes on crashing it simply exits the app but other times it lands on some previous activity. Is this a known behavior or is there any logic behind this which can be handled. Tried to search it on SO but couldn't find right combination of keywords for my problem.
Thanks
It depends on the FLAGS you created the Intent with. For example, with CLEAR_TOP If the activity being started is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent()).
When an Activity crashes if there are no more back-stacked activities of the same app, it will simply exit and show the device's home.

Starting location setting page

In my app I need to bring up the location settings page if location is not enabled.
This is done by
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
It works OK, however, once the setting page is finished with, I find I have to tap the back button several times to get back to my app, and on an HTC phone, the setting page sometimes crashes (my app is OK).
Other apps that do this do not seem to have these problems, are there any suggestions as to what they do that I don't?
The code aove is called directly from a View touch event.
In your activity, before starting location source settings, register a broadcast receiver for android.location.PROVIDERS_CHANGED and when you received the broadcast start your activity and unregister your receiver. You might also want to make your activity single-instance.

android hidden app strategy

I'm trying to create an app "hidden" from applications list.
The way i though the user will start the app is through a Receiver listening for NEW_OUTGOING_CALL and intercept a particular number dialed.
The problem is that on new Android versions, this receiver will never be activated if the app never start once. (Starting the application from a BroadCastReceiver (NEW_OUTGOING_CALL doesn't always work)).
I can't figure out a workaround for this problem: the app launcher is totally hidden so the user cannot never launch the app, and the receiver would never be activated if the app will never start.
Is there any other strategy or workaround for hide and launch the app with some kind of secret action?
Create a activity with manifest file pointing it as the the Launcher activity and make it transparent and call its finish method in onCreate. User clicking on that icon will have no idea that the activity is opened. But why don't you show the About application kind of screen in the launcher activity?

Will an application launch if it is already running?

I am making an app and I am putting the final touches on it and i was wondering, if the user presses the "home" button while the application is running, then clicks on the icon to try to run it again, what will the app do? Will it bring the current app to the front? Will it run a new instance? I am wondering because my app has a lot of threads and if the user was running 2 instances of my app it would kill their battery life.
It depends on the android:launchMode of your activity, set in the Manifest.
With the default setting the system always creates a new instance of the activity in the target task and routes the intent to it.
See the reference for detailed info.

Categories

Resources