Android app exit, which Mechanism to use? - android

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

Related

Finish Theme.Transluscent.NoTitleBar?

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

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.

Saving State With Android

I have an app that will never require more than one instance of an activity. I want it so that when the user comes back to a screen it is in the same state as they left it except for a few places where it doesn't make sense. I've worked out saving the persisted data with onpause onstop updates. However to keep the screen looking the way it did when they left it i use intents specifically setting the flags to Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_SINGLE_TOP then startActivity. It seems to work great but does it make sense? Is there a smarter way? Pitfalls doing it this way etc... any feedback will be greatly appreciated.
android:launchMode = "singleTask"
add the above line for every activity in the manifeast file. Adding these launch relaunch the activity instead of creating the activity again.
Refer this link

How to ignore Activity life cycle in Android?

I just want to ignore the Activity life cycle.
I mean that I want to make the Activity is always on top of the window.
The Activity is not be destroyed even if the other Activity or App is executed.
No, you can't do that. It violates the Android Framework.
If you just want to do something all the time, in the background, you should use a Service.
Your words are really contradictory.
You want to ignore Activity life-cycle, which means you wanna run wild out-of-the-box
The Activity is not be destroyed even if the other Activity or App is executed. <--- yes, of course, from Activity A you start Activity B, Activity A is still alive according to the Activity life-cycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You can't do that without changing the Android Code.

variables retaining values after app close

My app is retaining all of the variable values when it closes and this is effecting how it runs when reopened. Is there any way to reset them all upon app close? or is there a way to clean the app from memory when it is closed so to speak? For the moment I have just been setting all of the important variables "=0" in the last few lines of execution but I know there must be a correct way to doing this.
EDIT:
OK I thought that it would just be easier to reply here instead of individually to everyone.
The app is indeed staying alive in the background, I checked with advanced task killer. How would I get the ap to "Die" by presing the back button? I think this would be the easiest solution given how the app works:
open app > press start button > press stop button > results screen > press back button to exit.
so basically each time the app runs should be an independant execution.
Override the onPause, onResume, and onDestroy methods. onPause should save anything upon pausing, onResume should reload these values when it is resumed, and onDestroy will be called when your app closes. You can clean up stuff in onDestroy. See this link.
You app is probably not closing but remaining in background. Check advanced task manager and see if the app is running or not.
You need to familiarize yourself with the Activity Lifecycle.
You could leverage onResume() to reset your variables; also note onDestory() and onPause().
UPDATE:
Killing the application in its entirety each time the app moves to the background is an anti-pattern. You should really look at your application and follow the aforementioned activity lifecycle pattern and take the needed steps to insure your variables exist as you desire based on state.
I like what #Alex and #Jack said. To add to that, also consider that you can call finish() in your Activity if you want to force it to close up and return to the last Activity. Going along with this, also consider the use of setResult(int) (JavaDoc Here)
You can also set a flag on the Intent when you call the Activity you are questioning about. A flag like FLAG_ACTIVITY_NO_HISTORY could be helpful:
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.
List of Intent Flags
Uninitialized variables are bad. Don't do it. ALWAYS manually reset variables before using them for the first time.
the onResume() method will let you reset the variables when the program resumes, but will also do it when you return to the activity unless you add the logic that says you are coming from in the app, not the home page. Maybe onRestart() is what you really need? I'm not positive, but it's possible with onResume.

Categories

Resources