Android: reset if reopened after launching external Intent - android

My app requires location services to be enabled.
I wrote a loader Activity that verifies the different providers, and launches the system settings interface if something needs reconfiguring, much like Google Maps does.
Intent goToSettings = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(goToSettings);
Problem is, if the user navigates away without finishing the settings panel, it's left at the top of the Activity stack and shows up again when the app is brought to foreground (on relaunch, maybe in other scenarios).
How can I "detach" the system settings from my app? I tried combining flags and launch modes, but I can't get it working.

Use intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); to launch the external intent as a separate task.

Related

Is it possible in Android to keep app in background alive

There is a Xamarin app that I didn't develop and I don't have the source code for.
It's important that android OS will not kill or put this app to sleep if the user didn't use it for a 2-3 hours.
Can I develop another small app or service that will help keeping this app alive?
I already red online articles and tried changing battery optimization settings. Nothing worked.
there are no methods in API for manipulating other apps lifecycle, system decides when it is needed to kill some app. maybe you can introduce some own custom AccessibilityService, which will start this target app foreground (simplest way: open launcher Activity, just start app as usual):
Intent intent = context.getPackageManager().getLaunchIntentForPackage("target.app.package");
startActivity(intent);
and then after few seconds "home" it using below
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
note that this operation will be visible for user

Android Intent survives through app relaunch / Airbnb deep link survives through app relaunch

We used this Airbnb deep link dispatch lib in our app.
https://github.com/airbnb/DeepLinkDispatch
Below is the issue app is facing.
Kill app.
Launch app with deep link. That opened, say, a dialog on top of activity.
press back key on app and close it.
Launch it again through Recent Tasks.
Dialog shows up again. Repeat steps, it shows up again.
This is mostly because on back key, process is not dead and activity has overrides onSaveInstanceState() and onRestoreInstanceState(). I see in onCreate() that getIntent() returns the intent with same previous deep link. Hence the problem.
This does not happen if I launch app from home screen in which case it posts the launch home intent so the previous intent could have been replaced with it and so gone.
I know it is how it works in Android. But is there a way in this lib or in Android to solve it ?
It was not an issue with Airbnb library but that is how it works with Android. When launched from recents after killing app with back key, Android posts the previous intent that was used to launch app.
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS indicates if app launched from history/recent screen. This flag can be used to handle any logic.
Credits to Android: Activity is using old intent if launching app from Recent Task

How does this code to move the app to the background work?

I wanted a way to exit my app. Hence I searched and found a piece of code which does that.
But I am not able to understand the code and why it does what it does.
Can anyone please explain?
Here is my code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
It's starting the action ACTION_MAIN that has the category CATEGORY_HOME. This corresponds to:
This is the home activity, that is the first activity that is displayed when the device boots.
So, this means the launcher. So, this is a fancy way of starting the launcher app.
Also, the flag FLAG_ACTIVITY_NEW_TASK means it will be started on a new task, not the same as your app is running.
Notice that this will not finish your app, it will just put it in the background (like switching to another app using the task switcher, just this app happens to be the launcher). The effect is the same as pressing the home button.
Also notice that if the user happens to have two launcher apps, and it has not selected the default one, it will get a chooser asking to select which one of the two (or more) apps wants to use. Not the best experience.

Android - Open up Bluetooth setting, but in another view

I know that to open up the Bluetooth settings programmatically.
I do something like this:
Intent intentOpenBluetoothSettings = new Intent();
intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(intentOpenBluetoothSettings);
But doing so will take me to the Bluetooth settings page in the same view/application.
How should I go about if I want the Bluetooth settings page to open up in another view/window/page, outside the application?.
The reason why I want this to be done is so that the user will not confuse the settings page and my app.
Thanks.
Update
I tried doing intentOpenBluetoothSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
But no luck in getting it to open in another view.
There are two ways of requesting for enabling Bluetooth:
intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
or
intentOpenBluetoothSettings.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
If none of them suit your needs, then you might rethink your idea.
I advice reading up a little on android activity lifecycle and backstack.
Settings is NOT running in your app.
But normally everything you start is put on the backstack.
So if you go from your app to some other app (settings) android will remember that.
Your app there is not destroyed but only set on pause (onPause is called).
If you close settings it will return to your app and delete the entry for settings on backstack. I assume that is not the problem here.
But if you don't close settings and then by some means try to start your app again it is not destroyed but only waked up (no onCreate but only onResume). Android will then check that your app has loaded settings currently and settings will show up again.
If you restart your app with an Intent you control (like from a service)
You can:
startMyAppIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
The flag will kill the backstack up to the point where your app is on top of the stack so your app will start no matter if it had started settings before.
Backstack is not cleared completely so if your app was started by another app you may be able to return to that when pressing the back button.

Exit app in Playbook/BB10

To exit an app programatically in Android (for instance, if the user presses an exit button), I use:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
However, the CATEGORY_HOME intent category is not supported when porting Android apps for Playbook or Blackberry 10. What should I use instead?
I think your "exit an app code" for your Android app is quite the opposite of exiting the app: You start a new activity for the home screen instead of finishing "your own" activity.
You should instead finish your activity like this (and consider if that applies to more than one activity on your stack):
myAppView.finish()
This would also "quit" your application on bb10 - which means, it will be minimized and shown as an active frame until you tip on the X to close it.
As well, it would be a good idea to decide if you should clear outstanding notifications at this time (for my app, this makes sense...)

Categories

Resources