D-D
D-D

Reputation: 966

Disabling and enabling back button

In the app i m looking for the users location using gps.In the Async pre execute method i m showing a toast.I want that while i show that toast the back button should be disabled

aftr the location is found i want to enable the back button in the post execute!

to disable the back button i have used.But this is not working

 OnKeyListener mainScreenKeyListener = new OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    boolean disableEvent = false;
                    if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
                        disableEvent = true;
                    }

                    return disableEvent;
                }
            };

Upvotes: 2

Views: 6210

Answers (4)

Mohd Mufiz
Mohd Mufiz

Reputation: 2236

override onBackPress method in your activity

Class A
{
    public static boolean isToastShown=false;



    @Override
        public void onBackPressed() {
               if(isToastShown==true)
                 return false;
               else
                 super.onBackPressed();

             }

}

@Override
    protected void onPreExecute() {
        super.onPreExecute();
           //Show your toast here
           A.isToastShown=true;


new CountDownTimer(2000,2000) {

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onFinish() {
                A.isToastShown=false;

            }
        }.start();


      }

Upvotes: 0

Vipul
Vipul

Reputation: 28103

Here you go

Assign one static variable.and set its value to "NO" in onPreExecute. in onPostExecute assign its value to "YES".

And write following code in your onBackPressed.

 @Override
    public void onBackPressed() {

       if (decision.equals("NO")) {  //Here no means dont allow user to go back

       } else {
           super.onBackPressed(); // Process Back key  default behavior. 
       }

   }

Upvotes: 2

appukrb
appukrb

Reputation: 1507

hi for disable you simply call the above function

public void onBackPressed()
        {
         }

for enable

 public void onBackPressed()
    {
        super.onBackPressed();
        super.finish(); 
        //Intent
    }
 if you want both set flag inside the function

Upvotes: 1

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You can declare global variable disableEvent by

final boolean disableEvent;

Your Preexecute method can set it to false by

disableEvent = false;

Your Postexecute method can set it to true by

disableEvent = true;

You can override onBackPressed as shown below:

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        if (disableEvent)
        {
            // do nothing
        }
        else
        {
            // do something
        }
    }

Upvotes: 4

Related Questions