want to close application completely - android

i want to close my whole application. i have tried out finishActivity() on back press but it doesn't work. I want to close my application on back press event completely. How could i do it?
public void onImageViewClicked(View view){
switch(view.getId()){
case R.id.viewStk:
intent = new Intent(MainActivity.this,ViewStkActivity.class);
startActivityForResult(intent, 12);
break;
case R.id.about:
Intent aboutIntent = new Intent(MainActivity.this,AboutActivity.class);
startActivity(aboutIntent);
break;
}
}

I use:
#Override
public void onDestroy()
{
super.onDestroy();
// Force the process to be killed on finish(), not kept around
System.runFinalizersOnExit(true);
System.exit(0);
}
which is called after using this.finish()
(This is an overridden method of the activity)
People will complain and tell you it breaks the Android app lifecycle, but having an "exit" button on my app during development was an immediate boost of my productivity.
Edit
Apparently, the latest version of the API notes that the runFinalizersOnExit function is deprecated and unsafe. I've had no issue with it on Gingerbread, ICS, or JB. But in interest of full disclosure, I'll point this out.

public void onClick(View v) {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}

This will also help
Intent i = new Intent(yourScreen.this,Home.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", true);
startActivity(i);
and in the onCreate of the Home class, do this to check,
if (getIntent().getBooleanExtra("EXIT", false))
{
finish();
}
what this will essentially do is no matter at what activity you are, you can call the home screen with the clear top flag. In the home screen there is a check condition in the onCreate method which will help to clear the stack..Now ,if you press back button you will exit the app as the stack is cleared..
Let me know if the problem still persists...

In the activity.java-file that needs the implementation (fx MainActivity.java) insert this:
public void exitProgram(View view){
finish();
System.exit(0);
}
Just remember to also call the function in your .xml file, fx:
<Button
...
...
android:onClick="exitProgram"
...
/>
and finally in the buttons attributes, at the "text" attribute - press the small button next to the field and choose the one for the button you want to work with - done.
A good way to do it? No. Not at all. But if you just want a "panic-lets-get-outta-here"-button, then here you go.
There are a ton of different ways to implement the exit-functionality (one of them shown here) - as there are a ton of ways to implement the simple "Hello World" program - each has their value and you should really consider, why you have chosen the one you have.

Related

Trying to reload an Android activity from itself not working

My problem is: I can't figure out a way to restart my activity from itself. For example in a game, if you die, there's a menu with an option to retry, which restart the stage instantly. That's exactly the case.
As i have read from a lot of others answers on this matter, there are 2 main methods for reloading an activity:
1- finish();
startActivity(getIntent());
2- recreate();
The first method doesn't seem to work for me because it restart the activity, but leave the previous created one open on the back of my stack. Looks like the finish() statement is not doing its stuff. I have tried to put it in front, after, and all kind of ways, but it never closes the activity left behind.
On the other hand, the option number 2 cannot be called (as far as i know) from outside the UI thread, and if i do, the app crashes. I tried to call the recreate method using:
Activity.this.runOnUiThread(new Runnable() {
public void run() {
recreate();
}
});
But this approach gives me a very awful blinking on my screen. A black screen suddenly appears, and that doesn't seems right.
Try this:
Intent intent = getActivity().getIntent();
getActivity().finish();
startActivity(intent);
You should add FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK flags to the intent, such as:
Intent intent = getActivity().getIntent();
intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
getActivity().finish();
startActivity(intent);
This will force the system to remove the previous task from the stack.

Programmatically simulate App being destroyed by OS from GUI button press

I would like to programmatically simulate my app being destroyed, for debugging. However for convenience, I would like this to happen by pressing a button on the app's GUI. At the moment, I have the following in a fragment:
public void onClick(View v) {
switch (v.getId()){
case R.id.exitButton:
Log.i(TAG, "Exit pressed");
finish();
System.exit(0);
}
}
The problem is, onDestroy() never gets called. In fact, nor does onPause() either. (Or at least LogCat doesn't show the logs I write in them.) What am I doing wrong? (If I press the home button, onPause() does get called, so I don't think it's a problem with my onPause() or onDestroy() method
Thanks
Thanks to MysticMagic's link, and the adding of finish(), The following finally worked:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

onBackPressed causing problems in code

I have following code in an activity SCORE:
#Override
public void onBackPressed() {
Intent i = new Intent(Score.this, MainActivity.class);
startActivity(i);
}
Basically when user hits back i want them to go back to main activity. In main i have button when hit it exits the app something like this:
case R.id.exitButton:
finish();
break;
So when i start app and hit exit button it works fine it exits the app. But when i am at score page and hit back and than hit exit button app does not exit it goes back to score page. Can anyone please tell what am i doing wrong?
You starting the MainActivity from Score. Score is not being finished so the Score activity is still active and on the navigation stack.
try to do the following:
#Override
public void onBackPressed() {
Intent i = new Intent(Score.this, MainActivity.class);
startActivity(i);
finish();//Finishing the score activity is needed
}
As stated in above comment it is not normal to have an exit button, but you are free to implement this if you want and or needed. For more information about the back stack have a look at the following android documentation.
http://developer.android.com/guide/components/tasks-and-back-stack.html
also there are various methods to to go back to an active activity have a look at Intent flags. Such as FLAG_ACTIVITY_CLEAR_TOP at the following documentation page:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
i think the problem is you use the finsh() method to finish the activity.
may be you can use method like below:
case R.id.exitButton:
System.exit(0);
break;
hope that helps you.
For going to the main-activity after clicking back from score you want to add the following..
#Override
public void onBackPressed() {
finish();
}
And for exit from that app after clicking exit button add the following.
case R.id.exit:
System.exit(0);
break;
Hope this helps you..

How to exit from application permanently in Android

I am doing one application here I have one exit button when I click button,that time I need to exit from application..I tried using below code app is closing but it still running in taslmanager..but when I click exit button I should close app as well as I should remove from task manager how it's possible.
public class MainMenu extends Activity {
Button exit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
exit=(Button)findViewById(R.id.btn1);
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
try this to exit the application
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
finish();
Please think really hard about if you do need to kill the application: why not let the OS figure out where and when to free the resources?
Otherwise, if you're absolutely really sure, use
finish();
As a reaction to #dave appleton's comment: First thing read the big question/answer combo #gabriel posted: Quitting an application - is that frowned upon?
Now assuming we have that, the question here still has an answer,
being that the code you need if you are doing anything with quitting
is finish(). Obviously you can have more than one activity etc etc,
but that's not the point. Lets run by some of the use-cases
You want to let the user quit everything because of memory usage and
"not running in the background? Doubtfull. Let the user stop certain
activities in the background, but let the OS kill any unneeded
recourses. You want a user to not go to the previous activity of your
app? Well, either configure it so it doesn't, or you need an extra
option. If most of the time the back=previous-activity works,
wouldn't the user just press home if he/she wants to do something
else? If you need some sort of reset, you can find out if/how/etc
your application was quit, and if your activity gets focus again you
can take action on that, showing a fresh screen instead of restarting
where you were. So in the end, ofcourse, finish() doesn't kill
everthing, but it is still the tool you need I think. If there is a
usecase for "kill all activities", I haven't found it yet
Use this flag:
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS.
This flag will notify the OS to remove this app/activity from the cache of recent apps.
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
You can also add
android:noHistory="true"
android:excludeFromRecents="true"
in your AndroidManifest file for the activity where you don't want to see in recent apps.
Android doesn't allow you to directly exit from the app.you just have to remove the your current activty from the stack before going to any new activity.
class first extends activity{
oncreate(){
Intent i = new Intent(getappcontext(),nextactivity);
stratactivity(i);
first.this.finish();
}
}

How to exit when back button is pressed?

When a user presses the back button on an intent, the application should quit. How can I ensure the application quits when the back button is pressed?
In my Home Activity I override the "onBackPressed" to:
#Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
so if the user is in the home activity and press back, he goes to the home screen.
I took the code from Going to home screen Programmatically
Immediately after you start a new activity, using startActivity, make sure you call finish() so that the current activity is not stacked behind the new one.
EDIT
With regards to your comment:
What you're suggesting is not particularly how the android app flow usually works, and how the users expect it to work. What you can do if you really want to, is to make sure that every startActivity leading up to that activity, is a startActivityForResult and has an onActivityResult listener that checks for an exit code, and bubbles that back. You can read more about that here. Basically, use setResult before finishing an activity, to set an exit code of your choice, and if your parent activity receives that exit code, you set it in that activity, and finish that one, etc...
A better user experience:
/**
* Back button listener.
* Will close the application if the back button pressed twice.
*/
#Override
public void onBackPressed()
{
if(backButtonCount >= 1)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}
The app will only exit if there are no activities in the back stack. SO add this line in your manifest android:noHistory="true" to all the activities that you dont want to be back stacked.And then to close the app call the finish() in the OnBackPressed
<activity android:name=".activities.DemoActivity"
android:screenOrientation="portrait"
**android:noHistory="true"**
/>
Why wouldn't the user just hit the home button? Then they can exit your app from any of your activities, not just a specific one.
If you are worried about your application continuing to do something in the background. Make sure to stop it in the relevant onPause and onStop commands (which will get triggered when the user presses Home).
If your issue is that you want the next time the user clicks on your app for it to start back at the beginning, I recommend putting some kind of menu item or UI button on the screen that takes the user back to the starting activity of your app. Like the twitter bird in the official twitter app, etc.
Use onBackPressedmethod
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
This will solve your issue.
First of all, Android does not recommend you to do that within the back button, but rather using the lifecycle methods provided. The back button should not destroy the Activity.
Activities are being added to the stack, accessible from the Overview (square button since they introduced the Material design in 5.0) when the back button is pressed on the last remaining Activity from the UI stack. If the user wants to close down your app, they should swipe it off (close it) from the Overview menu.
Your app is responsible to stop any background tasks and jobs you don't want to run, on onPause(), onStop() and onDestroy() lifecycle methods. Please read more about the lifecycles and their proper implementation here: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
But to answer your question, you can do hacks to implement the exact behaviour you want, but as I said, it is not recommended:
#Override
public void onBackPressed() {
// make sure you have this outcommented
// super.onBackPressed();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
To exit from an Android app, just simply use.
in your Main Activity, or you can use Android manifest file to set
android:noHistory="true"
finish your current_activity using method finish() onBack method of your current_activity
and then add below lines in onDestroy of the current_activity for Removing Force close
#Override
public void onDestroy()
{
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
I modified #Vlad_Spays answer so that the back button acts normally unless it's the last item in the stack, then it prompts the user before exiting the app.
#Override
public void onBackPressed(){
if (isTaskRoot()){
if (backButtonCount >= 1){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}else{
super.onBackPressed();
}
}
you can simply use this
startActivity(new Intent(this, Splash.class));
moveTaskToBack(true);
The startActivity(new Intent(this, Splash.class)); is the first class that will be lauched when the application starts
moveTaskToBack(true); will minimize your application
Add this code in the activity from where you want to exit from the app on pressing back button:
#Override
public void onBackPressed() {
super.onBackPressed();
exitFromApp();
}
private void exitFromApp() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}

Categories

Resources