Finish Theme.Transluscent.NoTitleBar? - android

I have an android activity with Theme=Theme.Transluscent.NoTitleBar.
I want to use finish this kind of activity using finish() but it doesnt work. As it is an invisible activity it goes to onPause State rather than onDestroy state.
How can I overcome this problem as it uses unnecessary memory.
Thank YOU !!

There are many threads which explain this. When you are calling finish(), Android will let your code in the specific block after the finish() call execute, and that is why the Toast message appears. A simple return statement after the finish() call is the solution. Taken from answer on question:
Android Help! I want to completely finish my activity, and after activity.finish no further code will execute?
Other relevant ones are:
Calling finish() on an Android activity doesn't actually finish
about finish() in android
Hope this helps.

you can use System.exit(0); but as stated it is not a good approach. but however you can use this approach to clear the previous activity intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
.

Related

What does this.finish() really do? Does it stop my code running?

I'm a bit hazy about what exactly this.finish() does. Specifically, I just wrote the following lines of code in an activity:
this.finish();
Globals gs = (Globals) getApplication();
gs.MainActivity.finish();
The code is meant to close the current activity and also close the core activity of the app. And it works great. However, I got wondering... obviously the current activity isn't quite ended after the first line is executed. And what if I was to call this.finish() and then start on some complicated computation?
My question is: When I call this.finish(), when exactly does my Activity get taken down?
Whatever method called finish() will run all the way through before the finish() method actually starts. So, to answer your question, after the calling method finishes then your activity will run its finish method.
If you don't want the method to continue then you can add a return statement after finish
I'm a bit hazy about what exactly this.finish() does
Calling finish() basically just notifies the Android OS that your activity is ready to be destroyed. Android will then (whenever its ready) call onPause() on the activity and proceed to destroy it (no guarentee onDestroy() will be called), via the activity lifecycle. In general, you probably should not be doing any more execution after you call finish(). According to the Android docs, you should call finish() when you are ready to close the activity.
when exactly does my Activity get taken down?
I am guessing your activity will simply be added to some destroy queue. During this time you might be able to continue executing until the OS destroys it. I believe you are for sure allowed to finish executing the method from which finish() was called.
Activity.finish() will not stop the current activity until the method is completely executed so to skip the remaining part of the code, you may use a return; use it with some condition to validate your skip.
if ( condition = true ) {
this.finish();
return;
}
Chris, I am no expert, but at the answer here about finish() in android is basically what codeMagic just said. The link is valuable because of the discussion regarding onStop() and onDestroy()

Activity is stopped when starting new activity with a custom Camera implementation

When starting a new activity that has a custom Camera implementation, the main activity is closed (onStop is called with IsFinishing() set to true). When calling other activities this does not happen. I am working under the assumption that the main activity is being closed due to a low memory condition, as I can start other activities without error. How do I prevent the main activity from being shutdown when I call the camera activity, as there is a service started in the main activity that will be re-used for the camera activity?
Code that calls new activity:
startActivity(new Intent(Context, MyClass.Snapshot.class));
Try using startActivityForResult to signal to Android that you want your Activity to be delivered a result.
I don't think keeping the MainActivity from closing is a viable option. You stated that the reason is related to the MainActivity starting a service. Well really anything with a a reference to a Context can start a service. You could use a Singleton quite easily. I think keeping the Main Activity around is not necessary, and not a good practice since Android can always decide something like this. One thing you might try is to startSticky the Service and see if that makes a difference. Let us know.
The answer is my own stupidity. Had a bunch of commented code below my startActivity call, but I forgot to comment out 1 line towards the bottom... That line was calling onFinish() which would explain everything.
Thank you everyone for trying to diagnose my stupidity.

Android app exit, which Mechanism to use?

following are the ways to exits from the application
1. ActivityObject.finish();
2. Runtime.getRuntime().exit(0);
I want to know which way is to be used & when ?
if there is another way please let me know
Thanks in advance.
Shrenik
That's usually not a good idea at all to "exit" an application in android. That's against Android nature. Read this topic first before doing something like that.
Look at this life cycle of an Android activity:
And the description of the OnDestroy state:
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. You can
distinguish between these two
scenarios with the isFinishing()
method.
So calling ActivityObject.finish() is the right way to do it.
call moveTaskToBack(true) on your Activity

problem with finish() :Android

In an if statement, I have a finish() because I don't want the control to go any further. But, the app exits, but as I see from the logcat, the control goes beyond finish().
Maybe that's also the reason why a toast supposed to be displayed and some other GUI actions before finish() are not taking place?
Please advice.
onPause and onDestroy will be called after finish

Finish sub activities programmatically

I have an activity that launches another activity with startActivityForResult method. I would like to terminate the called one programmatically but I don't know how to do this since in onActivityResult() method I have no information about the called activity and I cannot call finish() on it. How can I achieve this?
Thanks
The launched Activity can finish self:
setResult(RESULT_OK);
finish();
Try finishActivity(requestCode). According to the documentation it lets you finish an activity previously started with startActivityForResult. And if there are multiple such activities with the same request code, all will be finished.
Note: I haven't actually tried this myself, but that's what the documentation says! Experiment with that, see if it does what you want.
At the moment you call startActivityForResult your Activity will be closed or paused and the new activity is started. The only one that can finish the new activity is the new activity.
You could start a background task and let this background task somehow notify your activity the activity could now finish itself.
I don't know if a Handler created in Activity A and passed to a thread will remain valid if Activity A is paused and Activity B is active. But I would assume this works because both of the Activities are running in the same thread therefore they should share the same message queue.
This thing just bit me, so I thought I'll add a comment here:
if(readyToFinish()){
finish()
}
thisCodeWillBeExecuted()
My experience is that all your code in the stacktrace gets executed, before
the activity is finished. The documentation is not ideal at this point.

Categories

Resources