Sumodh S
Sumodh S

Reputation: 741

How to end an application manually in android?

I want to end my app when user presses back key from the home screen. I tried using System.exit(0) but it doesn't seem to be working.

What do I have to do to end my application manually?

    package com.mahavega.qcdemo;


import com.mahavega.qcdemo.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {
     private GestureDetector gestureDetector;
        View.OnTouchListener gestureListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetector(this, new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };
        ImageView ad = (ImageView) findViewById(R.id.imageView1);
        ImageView im2 = (ImageView) findViewById(R.id.imageView2);
        ImageView im3 = (ImageView) findViewById(R.id.imageView3);
        ImageView im4 = (ImageView) findViewById(R.id.imageView4);
        ImageView im5 = (ImageView) findViewById(R.id.imageView5);
        ad.setOnClickListener(MainActivity.this);
        im2.setOnTouchListener(gestureListener);
        im2.setOnClickListener(MainActivity.this); 
        im3.setOnTouchListener(gestureListener);
        im3.setOnClickListener(MainActivity.this); 
        im4.setOnTouchListener(gestureListener);
        im4.setOnClickListener(MainActivity.this); 
        im5.setOnTouchListener(gestureListener);
        im5.setOnClickListener(MainActivity.this); 
        ad.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(MainActivity.this, Adds.class));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,    float velocityY)
        {
            float sensitvity = 50;
            if((e1.getX() - e2.getX()) < sensitvity){
                startActivity(new Intent(MainActivity.this, Login.class));
            }

            return super.onFling(e1, e2, velocityX, velocityY);
        }

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


    }
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        System.exit(0);
        finish();
        super.onBackPressed();
        }


   }

This is my whole code

Upvotes: 0

Views: 996

Answers (4)

surfealokesea
surfealokesea

Reputation: 5116

call moveTaskToBack(true) on your Activity (it exits your app from screen but doesn't destroy it)

Upvotes: 0

Jan Němec
Jan Němec

Reputation: 329

public static final void android.os.Process.killProcess(int pid)

Simple, works and does what you want. But please note that it is not recommended to exit application explicitly on Android. (Common GUI application just calls finish() on the top most Activity and lets on OS decision when to exit the application process.)

Upvotes: 0

mah
mah

Reputation: 39797

Depending on the version of Android you're running on / building for, you might need to add a little help to your Activity to catch the back button. Adding this function provides that help:

            @Override 
            public boolean onKeyDown(int keyCode, KeyEvent event)  { 
                if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5 
                        && keyCode == KeyEvent.KEYCODE_BACK 
                        && event.getRepeatCount() == 0) { 
                    onBackPressed(); 
                } 

                return true; 
            } 

Upvotes: 3

chopchop
chopchop

Reputation: 1933

In your Activity call finish(). Reference here https://developer.android.com/reference/android/app/Activity.html#finish()

Upvotes: 2

Related Questions