I would like to exit the app completely. I tried finish() and android.os.Process.killProcess(Process.myPid()) but they still leave app running in background.
A lot of people said that this is impossible, but my bank app is able to do that. When you exit that bank app, you can't see it in Recent Apps menu.
Does anyone know how to do it?
When you exit that bank app, you can't see it in Recent Apps menu.
That has nothing to do with a process. Processes and tasks are not particularly related.
Try finishAndRemoveTask() (API Level 21+), or perhaps finishAffinity() (API Level 16+), both methods on Activity. Or, as Thom Wiggers suggests in the comments, perhaps the app is not showing up in the recent-tasks list at all, via android:excludeFromRecents.
To remove activity from recents use this in the activity tag in manifest:
android:excludeFromRecents="true"
To remove application from background use below code. Please note that this will not remove the application from recents. It justs starts the application from the starting activity.
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
or
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Related
Originally, it received the BootComplete Action and tried to start automatically when the app completes booting. However, while checking because startActivity did not work, I found out that context.startActivity executed by the Action received from BoradcastReceiver does not work.
BroadcastReceiver
Intent startIntent = new Intent(context.getApplicationContext(),MainActivity.class);
startIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
It's a very simple code that I can't explain, but it gets Received, but the app doesn't start. The existing apps seem to work without problems. It feels like a ghost.
There is a log like this, but I don't know what the problem is.
D/BootReceiver: BootRecived
D/ZLA: Setting app side flag to false due to ActivityStarter-Normal Launch
Please add the following permission to manifest and ask for user permission once when the app opened the first time by calling
startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)); somewhere in your app :
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
if you are targeting/running on Android 10 then it isn't possible to startActivity, thats Android new policy (check out HERE)
now docs suggest to show Notification and user may pick it and this will start your Activity (or may remove it and your app won't start)
I use following code to stop my app:
this.finishAffinity();
However after I finish my app I can see in the application manager my app still has stop button enabled.Does this mean the app is still running?Moreover the android studio thinks app is running too.
If I finish the app with this code:
this.finish();
System.exit(0);
Android studio treats app as finished, but app manager still shows stop button enabled.
I read calling System.exit is unpreferable way of finishing the app.
What is the correct one?
In short, don't worry about it.
You should run checks to ensure you aren't leaking a Context, but in general you don't need to worry about your App staying "active".
This simply means that it hasn't been fully terminated yet, because Android hasn't needed to do so. Until then, it keeps it partially alive to be able to bring it back quickly if the user wants it again.
This doesn't take any additional processing resources to keep it alive. Only some memory. When Android deems it necessary to claim resources, it will kill your App fully.
Just keep using finish();.
The Android Lifecycle:
If you don't want your app to be shown in the overview screen when it finishes, simply add the android:autoRemoveFromRecents="true" to your activity tag in the manifest. No need to use System.exit(0).
To Quit Application on Button click use this code :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Try it..
To kill the complete app and remove it from Runningapp list kill the app through its pid(its nasty)... use this lines before above code.
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
no need to use this.finish();
just finish(); is ok
app manager always show the app is running because killing a app may occur misbehavior to its activitys.
public void EXIT(View view)
{
finish();
}
android.os.Process.killProcess(android.os.Process.myPid());
I have an application that starts the app selected by a user when it is clicked (like a launcher would). Note however that the actual starting of the app is performed by a service because of abstraction.
One note by a user:
Launching greader from your app opens greader as if it had never been used, prompting for account details etc regardless of whether they have been entered previously. Launching greader from any other method (nova laucher app drawer) gets me to my usual newsfeed.
Other users are reporting issues with root explorer crashing when started.
My best bet is that is has something to do with the launch flags. After investigating the default android launcher, my conclusion is that I am using the same flags.
These flags are: Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
What flags should I be using instead? I would not prefer to add exceptions for certain apps, so the flags should work for all apps to be started.
Thanks for any help
The problem was not with the flags themselves, they were just fine. This issue was with the action not being set properly. The problem was solved by initializing the intent like this:
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
I have application A and application B.
I launch B with an intent from A in this way:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.applicationB", "com.applicationB.MainActivity");
intent.putExtra(EXTRA_NAME,"name");
startActivity(intent);
When the user closes application A I want application B to close too. App B has JNI and uses exec() command.
I've tried android:sharedUserId but I got this error when app A tries to launch B with the code above:
Error running exec(). Command: [....] Working Directory: null Environment: (not null, it shows all the environment.
Any ideas?
you must use android IPC mechanisms like broadcast receiver
when application A closes it should send a broadcast and application B should register for a broadcast receiver to capture broadcasts from application A
see the documents for more info http://developer.android.com/reference/android/content/BroadcastReceiver.html
If both applications are programmed by yourself, you could consider to add a BroadcastReceiver in app B, that takes care, that all activities get closed. Before app A closes, call that receiver and it's done.
If you are not sure which activity is shown, you could extend the Activity class with a BroadcastReceiver, so that all activities get notified.
If app B is not programmed by you, you could use killbackgroundProcesses(), e.g.
ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(myProcessId);
... however, this works only if app B is in fact in background. Additionally this is not the best option to close an app 'cause you do not know what the app is currently doing.
Edit: found my favorite example on that topic :-) check http://www.hrupin.com/2011/10/how-to-finish-all-activities-in-your-android-application-through-simple-call - it shows how to close all activities from within the app. However, it's not a big step doing this from another one ...
According to Android docs:
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
"When the user leaves a task by pressing the Home button, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack."
If I understand this correctly, this means:
Activity A as MAIN Activity.
Activity B that gets started in A, through "startActivity" - common, plain intent instance.
I open app for first time, A gets loaded.
I click on button in A and B is openend.
I press home button.
I open app again, for 2nd time, and B is expected to be shown
...right?
I suppose this is the correct behavior to expect...
However, I am not seeing this in my app.
If I hit "Home button" and then resume my app, by pressing the launcher icon, it will start with the main activity - not the one at the top or latest one.
I am coding on a Samsung Galaxy Tab Android 2.2.1 - I have the most common options in the Android manifest - thing is that I handle like 10 different activities with different intent extras - and a Dispatcher class approach - or save each activity state - sounds quite demanding.
I am using Eclipse IDE with ADT version 12; and I found something very interesting:
When I run the app from the Eclipse IDE, with my device connected, I don't see this behavior. The app behaves as stated in the docs.
In fact, I saw this only after I deployed my apk at the Google Play app repository; and downloaded it to test.
My question is, has anybody found the real reason why is this happening?
Is the documentation wrong? or missing something?
Is this a bug on Android?
Another research I have done is:
When I try my app, downloaded from the google play, as APK, if I enter my app for the 2nd time, I get the "main" activity instead of the last one openend. I press home.
After pressing home, I enter application management settings for android, locate my app and click on "force stop".
After doing this, the app behaves as stated in the docs.
Somebody help! :)
This is a bug in android's platform:
http://code.google.com/p/android/issues/detail?id=2373
The workaround is, to place this in the onCreate method of your main Activity:
if (!isTaskRoot())
{
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN))
{
Log.w(LOG_TAG, "Main Activity is not the root. Finishing Main Activity instead of launching.");
finish();
return;
}
}
as extracted from:
How to prevent multiple instances of an activity when it is launched with different intents
...spent 3 days looking out for this.
I'm just going to explain why it fails, and how to reproduce this bug programmatically so you can incorporate this in your test suite:
When you launch an app through Eclipse or Market App, it launches with intent flags: FLAG_ACTIVITY_NEW_TASK.
When launching through the launcher (home), it uses flags: FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_BROUGHT_TO_FRONT | FLAG_ACTIVITY_RESET_TASK_IF_NEEDED, and uses action "MAIN" and category "LAUNCHER".
If you would like to reproduce this in a test case, use these steps:
adb shell am start -f 0x10000000 -n com.testfairy.tests.regression.taskroot/.MainActivity
Then do whatever is needed to get to the other activity. For my purposes, I just placed a button that starts another activity. Then, go back to the launcher (home) with:
adb shell am start -W -c android.intent.category.HOME -a android.intent.action.MAIN
And simulate launching it via the launcher with this:
adb shell am start -a "android.intent.action.MAIN" -c "android.intent.category.LAUNCHER" -f 0x10600000 -n com.testfairy.tests.regression.taskroot/.MainActivity
If you haven't incorporated the isTaskRoot() workaround, this will reproduce the problem. We use this in our automatic testing to make sure this bug never occurs again.
Hope this helps!
The docs are right, the only possible problem I can think of that is causing this is the device you are testing on, if it works as expected on the emulator (which is stock Android) it should work on at least 90% of Androids, its the manufactures fault for this I believe not Android.