Blog purpose for android basic example for android app developer. any query please my contact

Thursday 23 April 2020

android code to exit application

android code to exit application

in this Article today learn android code to exit application. you used this method your code. 


@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            AlertDialog.Builder alertbox = new AlertDialog.Builder(MainActivity.this);
            // alertbox.setIcon(R.drawable.info_icon);
            alertbox.setTitle("Are You Sure To Exit App");
            alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    // finish used for destroyed activity
                    finish();
                }
            });

            alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    // Nothing will be happened when clicked on no button
                    // of Dialog
                }
            });



            alertbox.show();
        }
        return super.onKeyDown(keyCode, event);
    }

follow the android code to exit application


1.MainActivity:


package com.sql.commands.app;

import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView btntutorial,btncommands,btninterview,btnrate,btnshare,btnpolicy;
    final Intent[] intent = new Intent[1];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
       
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            AlertDialog.Builder alertbox = new AlertDialog.Builder(MainActivity.this);
            // alertbox.setIcon(R.drawable.info_icon);
            alertbox.setTitle("Are You Sure To Exit App");
            alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    // finish used for destroyed activity
                    finish();
                }
            });

            alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    // Nothing will be happened when clicked on no button
                    // of Dialog
                }
            });



            alertbox.show();
        }
        return super.onKeyDown(keyCode, event);
    }

}



2.activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="22dp"
    android:background="#FCFCFC">
    
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Hello Developer"
       android:textColor="@android:color/darker_gray" />

                
</RelativeLayout>

No comments:

Post a Comment