Exit app in Playbook/BB10 - android

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...)

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

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.

How to make an app close itself completely (and cleanly)

I want to add "exit" functionality.
Display.getInstance().exitApplication(); almost works, but leaves
behind a "ghost" window in the android open applications view.
Why don't you finish all Activities and Services, and set every other reference to null.
That would leave you with just the Application Singleton alive if you have one.
Every Application on android OS, is like a User in Linux OS, and every application runs in a process which has a Process ID assigned to it which application does not know, but we if we kill the process and entire application will stop.
Here is how you can do.
android.os.Process.killProcess(android.os.Process.myPid());
but this would restart your first activity of your application, as android will try to recover the Application, you can avoid that by going to home screen like this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Display.exitAppliction is the only way to exit an app but it won't be removed from the task switcher. Thanks to the comment from #david-medenjak I was able to find this:
android:noHistory="true"
To use that just use:
android.xactivity=android:noHistory="true"
In your build hints and the app won't be in history at all. But as people said this is a pretty bad practice...
FYI in iOS if you include an exit button your app will be rejected by review unless its a "logoff" button. Typical mobile apps never really exit and are only supposed to exit for security purposes.
The OS will kill your app if it needs the resources. Notice that this is important since mobile apps are stopped/restarted often e.g. incoming phone call.

Launcher App chooser dialog is not being displayed

I want to display Launcher app chooser dialog whenever I want but It is not displaying , app is getting closed.
This is my code :
getPackageManager().clearPackagePreferredActivities(getPackageName());
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
If I set phone launcher as default by pressing always then it will not display the dialog , I am really confused why it is happening.
it will not display the intent chooser in 2 different cases:
if there is only one launcher app present in the android device.
the current launcher app is set as default.
I dont know if there are any other cases as well.
There is a 3rd possibility: the android OS is modified and does not respect the standards. I have this problem on a Huawei T1-701u tablet.
This tab has an extra screen for app standard settings. So the app-settings will always state "no standard set" and yet you will never see a chooser as the standard is controlled by a propietary system.

android exit application completely and go to main applications screen

i know that this question is asked many times, but really i can't understand the answer, i want to set button, when user click it i want to exit the application (also the carbage collector should remove the objects), and after exiting i want to go to the screen where the user found the application icon on mobile.
for exit i don't know what to do
for going to the screen where to find the application icon i tried like this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but doesn't work
use this method, this will make your application to go in backgroud and will open home/app_menu screen
moveTaskToBack(true); // method available in activity
reference link
When you start that activity, it will effectively stop your application's foreground activities. Android is designed to NOT shutdown the app so that it may be restarted faster.
However, if you really really want to purge the app from memory, then what you want to do is kill exit() on the Runtime singleton : http://developer.android.com/reference/java/lang/Runtime.html#exit(int)

Categories

Resources