I use startActivity() to begin a new Activity within my application and would like to find a way to either poll for or be notified when the started Activity exits. Is this possible?
Clarification:
The Intent passed into startActivity will be using ACTION_VIEW.
startActivityForResult() is the method you want to use.
After your Activity completes a callback will be made to the calling Activity for onActivityResult(), where you can then process the returned data from the closed Activity.
The documentation for Activity details pretty well how you can do this.
Related
I just came across this code in
Intent wizard = new Intent();
wizard.setClass(this, A.class);
wizard.putExtra("Domain", A.getInstance().B);
startActivity(wizard);
finish();
Why should we call finish() method here?
What's it purpose?
finish() Call this when your activity is done and should be closed, when starting an activity calling finish() will close the current activity and it will not be available in the Activity Stack.
finish();
finish is used for closing current activity.It is like you are sending data to other activity/Opening new activity and closing your current activity.
You are not imperative forced to do that...
normally people do that if and only if the want to destroy the activity that is starting the intent, but that must no be your case...
in the life cycle of android you will see:
assuming you activity is visible, then calling finish() method will call
onPause(), then onStop() and finally the onDestroy()
Let's understand it using few quotes
finish - Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you
via onActivityResult()
onDestroy() - The final call you receive before your activity is
destroyed. This can happen either because the activity is finishing
(someone called finish() on it, or because the system is temporarily
destroying this instance of the activity to save space.
when you call finish() on an activity, the methods followed by onDestroy() is executed.. eg: onPause()> onStop() > and onDestroy()they can be differ from the place you call finish()!
onDestroy() is meant for final cleanup - freeing up resources that you can on your own,closing open connections,readers,writers,etc. If you don't override it, the system does what it has to.
It inform the system that you want to finish the selected Activity so it wil call onDestroy() after you finish the activity.(but this does not mean that onDestroy() only gets called by finish(),system can do that when it's running out of resources after the activity is sent to the back state)
Also there are more interesting questions like Activity.finish() called but activity stays loaded in memory that you might like
I issue the a notification with an action that opens mainactivity. This is the first time the activity is called, created and everything works perfectly. After I pause the activity I automatically call onDestroy() of the activity and it is destroyed with success. When I click the notification for the second time, however, there is an error that says FATAL EXCEPTION: main can't resume activity. If the activity was destroyed I wasn't expecting it to be resumed but recreated again... why is this happening and how can I force the recreation of the activity by these methods?
Do not call onDestroy() directly!
These are lifecycle methods that the system should call. The problem you are having is that the system still believes the activity is paused (rather than destroyed as you would have liked) since it was never notified. You undoubtedly invoked super.onDestroy() within your onDestroy() method (the right thing) which caused part of the activity to clean itself up, making it non-resumable. When the system comes around the resume the activity in which it thought was paused, it throws that error.
Fortunately, there are ways to tell the system to kill your activity when you want it, through a method called finish().
Android documentation:
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
Replace your call to onDestroy() with this call should resolve your issue.
Never call lifecycle methods directly!
I have an Android library that requires a BroadcastReceiver, which I register in an object constructor. It performs actions on that object based on changing internet state. But, I'm encountering error messages about a leaked receiver, because I don't call unregister.
How and where can I call unregister when I have no information on when the Activity would end? Since I am only providing the class that would be instantiated by an Activity, so no onPause, onClose methods.
You could try saving your receiver in a static. I think only get this error if you register the same receiver twice.
If that fails, you might also try calling context.getApplicationContext().registerBroadcastReceiver() instead of using the activity context.
Shouldn't you be extending Activity, working properly with Android's workflow, and then have your users extend your Activity?
It's just a suggestion, but it would definitely solve the problem you're describing since you could register the receiver on onCreate() and unregister it on onDestroy().
Can an activity send itself an intent ?
Can an activity A send a "start" intent to start another activity B (onCreate() of B invoked) and B send again another start intent to A to make it appear again (instead of using finish()) ?
Is there another intent to make A appear again on foreground ?
If A has launched services with Java threads is there a risk that those threads are stopped/killed when another activity is started by A ?
Regards,
Apple92
Can an activity send itself an intent ? sort of. your next question is basically what I was going to tell you.
Can an activity A send a "start" intent to start another activity B (onCreate() of B invoked) and B send again another start intent to A to make it appear again (instead of using finish()) ? yes, by using a combination of lifecycles (see onPause() and onResume() methods) and startActivityForResult and onActivityResult(..)
Is there another intent to make A appear again on foreground ? what? you can whenever you want. You don't have to call finish() after you startActivity
If A has launched services with Java threads is there a risk that those threads are stopped/killed when another activity is started by A ? No. Only if Activity A is destroyed will that risk come up, and even then it is determined by how you start threads (a thread in a service will not be killed when its calling activity is killed)
What I want to be able to do is toggle the visibility of an element in my activity FROM a service.
How can I pass the current activity to my service class in order to send back messages/call functions in my activity?
What happens if the user exits the app and the service is still running, but obviously without a running activity to call functions within?
Thank you.
I don't think it's a good idea. Here's a good answer about notifying an activity from a service: Notify activity from service.
To answer your first question, you wouldn't be doing anything to the UI from your service. You have a few options for communicating between an activity and a service. You can set up a BroadcastReceiver. You can bind the service to your activity and use Android's AIDL to set up handlers for the communication. Plus others.
To answer your second question - make sure to stop your service in your onDestroy method. Alternately, if you've bound the service to an activity, the service will stop once the final activity that the service is bound to stops.