2

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;
                }
            };
2
  • Not sure of what you are willing to do. What do you mean exactly by enabling/disabling the back button? Please describe the expected behaviour of the app.
    – Shlublu
    Nov 19, 2012 at 12:38
  • Is your 'onkey' function executed? See what happens if you use onBackPressed: developer.android.com/reference/android/app/…
    – RvdK
    Nov 19, 2012 at 12:44

4 Answers 4

4

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
        }
    }
3
  • This method is thrwoing errors...syntax error on "void", @expected
    – D-D
    Nov 19, 2012 at 12:45
  • Don't copy it as it is. Just override onBackPressed Method and copy the contents of the method. Nov 19, 2012 at 12:49
  • It is not working is it becoz that my class already has a onbackpressedmethod. so when i call the on backpressed method inside my inner class its not taking that?
    – D-D
    Nov 19, 2012 at 13:27
2

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. 
       }

   }
1

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
1
  • Since i m using a an inner class so how do i make sure that onback pressed of the inner class is called
    – D-D
    Nov 19, 2012 at 13:16
0

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


      }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.