App is killed after launching an implicit intent - android

I like to test my apps with "don't keep activities" checked to make sure everything works in the worst case scenario where Android destroys my stuff. Today I noticed that this setting apparently can (and usually does) cause my entire application to be destroyed after I launch an implicit navigation intent. This is a problem because the original developer put a bunch of important background tasks in an extended Application instead of using a Service that Android would be more likely to keep alive when the app is in the background.
With "don't keep activities" checked, my entire app seems to be killed when the navigation activity starts. I no longer see logging from the background tasks, and when I go back to the activity that launched the navigation, it is recreated with a new, uninitialized Application. I can't simply re-initialize it because the main problem is that the tasks need to keep running while my app is in the background.
I'm not explicitly starting the navigation in a new task:
String encoded = Uri.encode(nextWaypoint);
Intent navigation = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + encoded));
startActivity(navigation);
Is the possibility of my Application being destroyed as unavoidable as the possibility of my activities being destroyed?

Your Application will be destroyed when there is no component using the process in which your Application was started. Since the "don't keep Activities" option will kill your Activity when you navigate away from it, and you do not have any other component making use of your process, your Application is terminated.
Going by what you are describing, your safest bet is to move your tasks to a Service

Related

When does Android destroy activity without destroying the entire process?

I want to understand and simulate when does Android call onDestroy() of my activity, without destroying the entire process. I'm not calling finish(), and I want to make Android destroy my activity on it's own.
From the activity-lifecycle documentation:
The system never kills an activity directly to free up memory. Instead, it kills the process in which the activity runs, destroying not only the activity but everything else running in the process, as well.
But the Android Activity documentation says:
This (onDestroy) can happen because [...] the system is temporarily destroying this instance of the activity to save space.
So which one is it? Does Android destroy activities when it's low on memory, or does it only kill entire processes?
I want to simulate a situation where Android kills the activity without killing the entire process.
I can mimic this by using the "Don't keep activities" developer-mode setting, but I want to understand how can this happen in the real-world.
I want to make Android destroy my activity on it's own.
Android does not do that, other than through your actions (e.g., finish()) or user actions (e.g., BACK navigation, configuration changes).
So which one is it?
The former (Android kills processes, not activities). See this answer from the woman who wrote this stuff. FWIW, also see this decade-old blog post of mine.

How to force Android to trigger activity kill

I am having an issue that's closely related to
support FragmentPagerAdapter holds reference to old fragments
and ViewPager and fragments — what's the right way to store fragment's state?
Anyway, my problem is that my application crashes on this one activity when it gets recreated after the system kills it. As it's a pretty heavy activity and I'm debugging to implement fixes, I need to trigger the "Activity killed by android system".
Right now, I am doing "Open 20 other apps, and then hope that my app gets killed before reopening it".
Is there any better way?
PS: I have tried killing it manually (force killing) from app information. It doesn't work, as my application gets recreated from my home screen
Actually I found an answer...
In developers settings, all the way down, look for
App -> Do not keep activities
Tick it, then launch your activity, leave it using homescreen, launch any other app (gallery or whatever), and then when you relaunch your app, it will have been killed by the android system
Calling the finish() method should work.
If you are inside of the context of the activity then simply call finish();
If you are outside of the context. Then pass the context of the activity and call
activity.finish();

how to force android system to recycle all background activities?

Is there API or commands to force Android system to recycle all background activities no matter there is enough resource or not? And how to check all the activities' status to check that the activity is actually killed?
There is an API called killBackgroundProcesses(), but this API is killed the whole process, I am wondering how to only kill some activities without killing the whole process.
As the android dev guide page says below, I am looking for the first way.
activity lifecycle
If an activity is paused or stopped, the system can drop the activity
from memory by either asking it to finish, or simply killing its
process.
Yes, there is a way:
Settings -> Developer options -> Apps -> Don't keep activities
Check that box, and you are good to go.
Cheers.
I don't think so Android gives you the information of Activities are in background (i.e in Paused or Stopped State). Even if Activity Stack as well will give you the access of activity at Top.
Possible Solutions :
1. If at all you want to destroy the service , better to call finish() after startActivity() method.
2. If you want to periodically destroy all the background activities. You should implement your own activity stack. Which does pushToStack() on start of new activity and popFromStack() and then activity.finish();

How to kill 3rd party process in android

My application will launch 3rd party map activity and set the place. Once user press back from Map activity, it will return to previous activity.
Once i quit the application, the map activity and other process will be keep running. i would like to close them. How should i do?
Thanks in advance
Try Launching the foreign activity with FLAG_ACTIVITY_NO_HISTORY
A process is not the same as an Activity, and it's not your concern if a process (even your own) remains alive when not in use, as Android will dispose of it when appropriate.
You should never try to kill foreign processes (you can't anymore anyway), and the situations where you want to kill or end your own are rare (mostly for testing to make sure you can recover).

How can I programmatically close an application?

I am looking for code for a button that completely closes my app.
I tried with some stuff from Google, but my app is still running in the background. I need to close it completely. Is there code that does this?
Why do you need to really close your app? Assuming it's just a normal app and not running any background services or holding a wakelock (you'd know if you were doing those things), the system does a very good job of task management and will end your app if it's backgrounded and it needs the RAM without any manual intervention. Normally if you just finish() your base Activity this will happen on its own, but there's almost never a reason to do that.
(The only exception to this is if your Application is somehow holding onto references to already-finished Activities, which can cause ugly memory leaks and keep your app from closing normally, but you'd also probably know if you're doing anything fishy with an overridden Application subclass.)
That is: 99% of the time if you want to forcibly close your Application, you either need to fix whatever bug in your code makes you think the system can't handle it on it's own, or you need to reread the documentation on the Android application lifecycle again (because you should have already read this 3 times before you started writing an Android app :)).
Maybe this link will help developer page
I quoted the part below that i think might help you.
Shutting down components
A content provider is active only
while it's responding to a request
from a ContentResolver. And a
broadcast receiver is active only
while it's responding to a broadcast
message. So there's no need to
explicitly shut down these components.
Activities, on the other hand, provide
the user interface. They're in a
long-running conversation with the
user and may remain active, even when
idle, as long as the conversation
continues. Similarly, services may
also remain running for a long time.
So Android has methods to shut down
activities and services in an orderly
way:
An activity can be shut down by calling its finish() method. One
activity can shut down another
activity (one it started with
startActivityForResult()) by calling
finishActivity().
A service can be stopped by calling its stopSelf() method, or by
calling Context.stopService().
Components might also be shut down by
the system when they are no longer
being used or when Android must
reclaim memory for more active
components. A later section, Component
Lifecycles, discusses this possibility
and its ramifications in more detail.
You can close all activities from background and when re-open the app It starts from first activity
this.finish();
Intent intent = new Intent(getApplicationContext(), CloseApp.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
You can close all activities from background and when re-open the app It starts from paused activity[where you closed] activity
this.finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Android doesn't like you closing your apps.
Here's a discussion on that:
Is quitting an application frowned upon?
If you really want to do it, for whatever reason, you need to close all your activities.
Here's a discussion on how you could do it:
Closing several android activities simultaneously
have in mind that:
finish();
method closes current Activity only.
If you have multiple Activities opened, you should call finish() per Activity.
Note: Closing Service is different.

Categories

Resources