How to prevent the activity from loading twice on pressing the button












60















I am trying to prevent the activity from loading twice if I press the button twice instantly after the first click.



I have an activity which loads on click of a button, say



 myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Load another activity
}
});


Now because the activity to be loaded has network calls, it takes a little time to load (MVC). I do show a loading view for this but if I press the button twice before that, I can see the activity being loaded twice.



Do any one know how to prevent this?










share|improve this question

























  • You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function

    – Maneesh
    Nov 10 '11 at 10:02











  • Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.

    – JimmyB
    Nov 10 '11 at 10:03











  • disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...

    – Awais Tariq
    Nov 10 '11 at 10:05













  • If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley

    – Shylendra Madda
    Feb 15 '18 at 6:42











  • Possible duplicate of Avoid button multiple rapid clicks

    – Arnab Kar
    Dec 11 '18 at 14:17
















60















I am trying to prevent the activity from loading twice if I press the button twice instantly after the first click.



I have an activity which loads on click of a button, say



 myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Load another activity
}
});


Now because the activity to be loaded has network calls, it takes a little time to load (MVC). I do show a loading view for this but if I press the button twice before that, I can see the activity being loaded twice.



Do any one know how to prevent this?










share|improve this question

























  • You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function

    – Maneesh
    Nov 10 '11 at 10:02











  • Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.

    – JimmyB
    Nov 10 '11 at 10:03











  • disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...

    – Awais Tariq
    Nov 10 '11 at 10:05













  • If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley

    – Shylendra Madda
    Feb 15 '18 at 6:42











  • Possible duplicate of Avoid button multiple rapid clicks

    – Arnab Kar
    Dec 11 '18 at 14:17














60












60








60


16






I am trying to prevent the activity from loading twice if I press the button twice instantly after the first click.



I have an activity which loads on click of a button, say



 myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Load another activity
}
});


Now because the activity to be loaded has network calls, it takes a little time to load (MVC). I do show a loading view for this but if I press the button twice before that, I can see the activity being loaded twice.



Do any one know how to prevent this?










share|improve this question
















I am trying to prevent the activity from loading twice if I press the button twice instantly after the first click.



I have an activity which loads on click of a button, say



 myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Load another activity
}
});


Now because the activity to be loaded has network calls, it takes a little time to load (MVC). I do show a loading view for this but if I press the button twice before that, I can see the activity being loaded twice.



Do any one know how to prevent this?







android button android-activity onclick






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 6:18







tejas

















asked Nov 10 '11 at 9:59









tejastejas

1,19072851




1,19072851













  • You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function

    – Maneesh
    Nov 10 '11 at 10:02











  • Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.

    – JimmyB
    Nov 10 '11 at 10:03











  • disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...

    – Awais Tariq
    Nov 10 '11 at 10:05













  • If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley

    – Shylendra Madda
    Feb 15 '18 at 6:42











  • Possible duplicate of Avoid button multiple rapid clicks

    – Arnab Kar
    Dec 11 '18 at 14:17



















  • You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function

    – Maneesh
    Nov 10 '11 at 10:02











  • Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.

    – JimmyB
    Nov 10 '11 at 10:03











  • disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...

    – Awais Tariq
    Nov 10 '11 at 10:05













  • If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley

    – Shylendra Madda
    Feb 15 '18 at 6:42











  • Possible duplicate of Avoid button multiple rapid clicks

    – Arnab Kar
    Dec 11 '18 at 14:17

















You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function

– Maneesh
Nov 10 '11 at 10:02





You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function

– Maneesh
Nov 10 '11 at 10:02













Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.

– JimmyB
Nov 10 '11 at 10:03





Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.

– JimmyB
Nov 10 '11 at 10:03













disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...

– Awais Tariq
Nov 10 '11 at 10:05







disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...

– Awais Tariq
Nov 10 '11 at 10:05















If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley

– Shylendra Madda
Feb 15 '18 at 6:42





If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley

– Shylendra Madda
Feb 15 '18 at 6:42













Possible duplicate of Avoid button multiple rapid clicks

– Arnab Kar
Dec 11 '18 at 14:17





Possible duplicate of Avoid button multiple rapid clicks

– Arnab Kar
Dec 11 '18 at 14:17












18 Answers
18






active

oldest

votes


















47














In the button's event listener, disable the button and show another activity.



    Button b = (Button) view;
b.setEnabled(false);

Intent i = new Intent(this, AnotherActitivty.class);
startActivity(i);


Override onResume() to re-enable the button.



@Override
protected void onResume() {
super.onResume();

Button button1 = (Button) findViewById(R.id.button1);
button1.setEnabled(true);
}





share|improve this answer
























  • This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…

    – Martin Marconcini
    Mar 5 '18 at 22:06



















119














Add this to your Activity definition in AndroidManifest.xml...



android:launchMode = "singleTop"





share|improve this answer


























  • if helped, then accept the answer..

    – Awais Tariq
    Nov 10 '11 at 11:48











  • ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)

    – Awais Tariq
    Nov 10 '11 at 12:51








  • 2





    what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..

    – Awais Tariq
    Nov 10 '11 at 17:38






  • 13





    This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.

    – lilbyrdie
    May 20 '14 at 15:19






  • 13





    This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would be android:launchMode = "singleTop", which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use the singleInstance option.

    – Nohus
    May 16 '17 at 18:06



















30














You can use the intent flags like this.



Intent intent = new Intent(Class.class);    
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);


It will make only one activity be open at the top of the history stack.






share|improve this answer





















  • 2





    this should be the correct android-way answer

    – rocketspacer
    May 16 '16 at 4:26








  • 3





    This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest: android:launchMode = "singleTop", this way it's solved without having to add the flag to every Intent.

    – Nohus
    May 16 '17 at 18:09



















11














Since SO doesn't allow me to comment on other answers, I have to pollute this thread with a new answer.



Common answers for the "activity opens twice" problem and my experiences with these solutions (Android 7.1.1):




  1. Disable button that starts the activity: Works but feels a little clumsy. If you have multiple ways to start the activity in your app (e.g. a button in the action bar AND by clicking on an item in a list view), you have to keep track of the enabled/disabled state of multiple GUI elements. Plus it's not very convenient to disable clicked items in a list view, for example. So, not a very universal approach.

  2. launchMode="singleInstance": Not working with startActivityForResult(), breaks back navigation with startActivity(), not recommended for regular applications by Android manifest documentation.

  3. launchMode="singleTask": Not working with startActivityForResult(), not recommended for regular applications by Android manifest documentation.

  4. FLAG_ACTIVITY_REORDER_TO_FRONT: Breaks back button.

  5. FLAG_ACTIVITY_SINGLE_TOP: Not working, activity is still opened twice.

  6. FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me.


EDIT: This was for starting activities with startActivity(). When using startActivityForResult() I need to set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.






share|improve this answer


























  • FLAG_ACTIVITY_SINGLE_TOP works for me

    – Alec von Barnekow
    Jun 6 '18 at 16:37











  • FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1

    – Mingjiang Shi
    Oct 19 '18 at 4:11











  • I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?

    – Mirmuhsin Sodiqov
    Jan 4 at 7:40











  • I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.

    – Mirmuhsin Sodiqov
    Jan 4 at 9:08



















5














Let's say @wannik is right but if we have more than 1 button calling same action listener and i click two buttons once almost same time before starting next activity...



So it is good if you have field private boolean mIsClicked = false; and in the listener:



if(!mIsClicked)
{
mIsClicked = true;
Intent i = new Intent(this, AnotherActitivty.class);
startActivity(i);
}


And onResume() we need to return the state:



@Override
protected void onResume() {
super.onResume();

mIsClicked = false;
}


What's the deifference between my and @wannik's answer?



If you set enabled to false in the listener of it's calling view other button using same listener will still be enabled. So to be sure that listener's action is not called twice you need to have something global that disables all callings of the listener(nevermind if it's new instance or no)



What is the difference between my answer and others?



They are thinking in right way but they are not thinking for future return to the same instance of the calling activity :)






share|improve this answer


























  • servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)

    – tejas
    Jun 24 '13 at 8:02








  • 1





    I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity

    – Sir NIkolay Cesar The First
    Jun 27 '13 at 12:52






  • 1





    This isn't sufficient. You need to use a synchronized(mIsClicked) {...} as well to be 100% safe.

    – Monstieur
    Dec 20 '13 at 12:59











  • That's not needed :)

    – Sir NIkolay Cesar The First
    Dec 21 '13 at 9:33











  • @Monstieur you don’t need a synchronized block because this is all Main Thread…

    – Martin Marconcini
    Mar 5 '18 at 21:41



















4














Use singleInstance to avoid activity to invoke twice.



<activity
android:name=".MainActivity"
android:label="@string/activity"
android:launchMode = "singleInstance" />





share|improve this answer































    3














    I think you're going about solving the problem the wrong way. Generally it is a bad idea for an activity to be making long-running web requests in any of its startup lifecycle methods (onCreate(), onResume(), etc). Really these methods should simply be used to instantiate and initialise objects your activity will use and should therefore be relatively quick.



    If you need to be performing a web request then do this in a background thread from your newly launched activity (and show the loading dialog in the new activity). Once the background request thread completes it can update the activity and hide the dialog.



    This then means your new activity should get launched immediately and prevent the double click from being possible.






    share|improve this answer































      3














      Hope this helps:



       protected static final int DELAY_TIME = 100;

      // to prevent double click issue, disable button after click and enable it after 100ms
      protected Handler mClickHandler = new Handler() {

      public void handleMessage(Message msg) {

      findViewById(msg.what).setClickable(true);
      super.handleMessage(msg);
      }
      };

      @Override
      public void onClick(View v) {
      int id = v.getId();
      v.setClickable(false);
      mClickHandler.sendEmptyMessageDelayed(id, DELAY_TIME);
      // startActivity()
      }`





      share|improve this answer



















      • 4





        lol - what if I click faster then 10x/sec? :D

        – Srneczek
        Oct 9 '15 at 14:19





















      3














      It was only working for me when startActivity(intent)



      intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);





      share|improve this answer































        2














        For this situation, I'll go for one of two approached, singleTask in manifest.xml OR a flag in the Activity's onResume() & onDestroy() methods respectively.



        For the first solution: I prefer to use singleTask for the activity in the manifest rather than singleInstance, as per using singleInstance I figured out that in some occasions the activity creating a new separate instance for itself which result to have a two separate applications window in the running apps in bcakground and besides extra memory allocations that would result a very bad User Experience when the user opens the apps view to choose some app to resume.
        So, the better way is to have the activity defined at the manifest.xml like the following:



        <activity
        android:name=".MainActivity"
        android:launchMode="singleTask"</activity>


        you can check activity launch modes here.





        For the second solution, you have to just define a static variable or a preference variable, for example:



        public class MainActivity extends Activity{
        public static boolean isRunning = false;

        @Override
        public void onResume() {
        super.onResume();
        // now the activity is running
        isRunning = true;
        }

        @Override
        public void onDestroy() {
        super.onDestroy();
        // now the activity will be available again
        isRunning = false;
        }

        }


        and from the other side when you want to launch this activity, just check:



        private void launchMainActivity(){
        if(MainActivity.isRunning)
        return;
        Intent intent = new Intent(ThisActivity.this, MainActivity.class);
        startActivity(intent);
        }





        share|improve this answer































          1














          Other very very simple solution if you no want use onActivityResult() is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:



             final Button btn = ...
          btn.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
          //start activity here...
          btn.setEnabled(false); //disable button

          //post a message to run in UI Thread after a delay in milliseconds
          btn.postDelayed(new Runnable() {
          public void run() {
          btn.setEnabled(true); //enable button again
          }
          },1000); //1 second in this case...
          }
          });





          share|improve this answer































            0














            Just maintain one flag in button onClick method as:



            public boolean oneTimeLoadActivity = false;



                myButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            if(!oneTimeLoadActivity){
            //start your new activity.
            oneTimeLoadActivity = true;
            }
            }
            });





            share|improve this answer































              0














              If you're using onActivityResult, you could use a variable to save state.



              private Boolean activityOpenInProgress = false;

              myButton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) {
              if( activityOpenInProgress )
              return;

              activityOpenInProgress = true;
              //Load another activity with startActivityForResult with required request code
              }
              });

              protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              if( requestCode == thatYouSentToOpenActivity ){
              activityOpenInProgress = false;
              }
              }


              Works on back button pressed too because request code is returned on event.






              share|improve this answer































                0














                Adding:



                android:launchMode="singleTop"


                Inside the activity tag in AndroidManifest.xml solved the issue.






                share|improve this answer































                  -1














                  myButton.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View view) {
                  myButton.setOnClickListener(null);
                  }
                  });





                  share|improve this answer
























                  • That would probably not work since you would have to declare it as final.

                    – King
                    Mar 29 '15 at 11:10



















                  -1














                  Use a flag variable set it to true,
                  Check if its true just return else perform Activity Call.



                  You can also use setClickable(false) one executing the Activity Call



                  flg=false
                  public void onClick(View view) {
                  if(flg==true)
                  return;
                  else
                  { flg=true;
                  // perform click}
                  }





                  share|improve this answer
























                  • perform click; wait; flg = false; for when we get back

                    – Xeno Lupus
                    Nov 10 '11 at 11:29





















                  -2














                  You can try this also



                  Button game = (Button) findViewById(R.id.games);
                  game.setOnClickListener(new View.OnClickListener()
                  {
                  public void onClick(View view)
                  {
                  Intent myIntent = new Intent(view.getContext(), Games.class);
                  startActivityForResult(myIntent, 0);
                  }

                  });





                  share|improve this answer
























                  • This does not work

                    – tejas
                    Nov 10 '11 at 12:48



















                  -2














                  You could just override startActivityForResult and use instance variable:



                  boolean couldStartActivity = false;

                  @Override
                  protected void onResume() {
                  super.onResume();

                  couldStartActivity = true;
                  }

                  @Override
                  public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
                  if (couldStartActivity) {
                  couldStartActivity = false;
                  intent.putExtra(RequestCodeKey, requestCode);
                  super.startActivityForResult(intent, requestCode, options);
                  }
                  }





                  share|improve this answer























                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8077728%2fhow-to-prevent-the-activity-from-loading-twice-on-pressing-the-button%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    18 Answers
                    18






                    active

                    oldest

                    votes








                    18 Answers
                    18






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    47














                    In the button's event listener, disable the button and show another activity.



                        Button b = (Button) view;
                    b.setEnabled(false);

                    Intent i = new Intent(this, AnotherActitivty.class);
                    startActivity(i);


                    Override onResume() to re-enable the button.



                    @Override
                    protected void onResume() {
                    super.onResume();

                    Button button1 = (Button) findViewById(R.id.button1);
                    button1.setEnabled(true);
                    }





                    share|improve this answer
























                    • This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…

                      – Martin Marconcini
                      Mar 5 '18 at 22:06
















                    47














                    In the button's event listener, disable the button and show another activity.



                        Button b = (Button) view;
                    b.setEnabled(false);

                    Intent i = new Intent(this, AnotherActitivty.class);
                    startActivity(i);


                    Override onResume() to re-enable the button.



                    @Override
                    protected void onResume() {
                    super.onResume();

                    Button button1 = (Button) findViewById(R.id.button1);
                    button1.setEnabled(true);
                    }





                    share|improve this answer
























                    • This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…

                      – Martin Marconcini
                      Mar 5 '18 at 22:06














                    47












                    47








                    47







                    In the button's event listener, disable the button and show another activity.



                        Button b = (Button) view;
                    b.setEnabled(false);

                    Intent i = new Intent(this, AnotherActitivty.class);
                    startActivity(i);


                    Override onResume() to re-enable the button.



                    @Override
                    protected void onResume() {
                    super.onResume();

                    Button button1 = (Button) findViewById(R.id.button1);
                    button1.setEnabled(true);
                    }





                    share|improve this answer













                    In the button's event listener, disable the button and show another activity.



                        Button b = (Button) view;
                    b.setEnabled(false);

                    Intent i = new Intent(this, AnotherActitivty.class);
                    startActivity(i);


                    Override onResume() to re-enable the button.



                    @Override
                    protected void onResume() {
                    super.onResume();

                    Button button1 = (Button) findViewById(R.id.button1);
                    button1.setEnabled(true);
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 '11 at 10:40









                    wannikwannik

                    8,23363453




                    8,23363453













                    • This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…

                      – Martin Marconcini
                      Mar 5 '18 at 22:06



















                    • This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…

                      – Martin Marconcini
                      Mar 5 '18 at 22:06

















                    This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…

                    – Martin Marconcini
                    Mar 5 '18 at 22:06





                    This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…

                    – Martin Marconcini
                    Mar 5 '18 at 22:06













                    119














                    Add this to your Activity definition in AndroidManifest.xml...



                    android:launchMode = "singleTop"





                    share|improve this answer


























                    • if helped, then accept the answer..

                      – Awais Tariq
                      Nov 10 '11 at 11:48











                    • ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)

                      – Awais Tariq
                      Nov 10 '11 at 12:51








                    • 2





                      what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..

                      – Awais Tariq
                      Nov 10 '11 at 17:38






                    • 13





                      This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.

                      – lilbyrdie
                      May 20 '14 at 15:19






                    • 13





                      This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would be android:launchMode = "singleTop", which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use the singleInstance option.

                      – Nohus
                      May 16 '17 at 18:06
















                    119














                    Add this to your Activity definition in AndroidManifest.xml...



                    android:launchMode = "singleTop"





                    share|improve this answer


























                    • if helped, then accept the answer..

                      – Awais Tariq
                      Nov 10 '11 at 11:48











                    • ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)

                      – Awais Tariq
                      Nov 10 '11 at 12:51








                    • 2





                      what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..

                      – Awais Tariq
                      Nov 10 '11 at 17:38






                    • 13





                      This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.

                      – lilbyrdie
                      May 20 '14 at 15:19






                    • 13





                      This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would be android:launchMode = "singleTop", which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use the singleInstance option.

                      – Nohus
                      May 16 '17 at 18:06














                    119












                    119








                    119







                    Add this to your Activity definition in AndroidManifest.xml...



                    android:launchMode = "singleTop"





                    share|improve this answer















                    Add this to your Activity definition in AndroidManifest.xml...



                    android:launchMode = "singleTop"






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 17 hours ago









                    Hans Knöchel

                    5,08271841




                    5,08271841










                    answered Nov 10 '11 at 10:04









                    Awais TariqAwais Tariq

                    6,12852548




                    6,12852548













                    • if helped, then accept the answer..

                      – Awais Tariq
                      Nov 10 '11 at 11:48











                    • ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)

                      – Awais Tariq
                      Nov 10 '11 at 12:51








                    • 2





                      what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..

                      – Awais Tariq
                      Nov 10 '11 at 17:38






                    • 13





                      This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.

                      – lilbyrdie
                      May 20 '14 at 15:19






                    • 13





                      This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would be android:launchMode = "singleTop", which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use the singleInstance option.

                      – Nohus
                      May 16 '17 at 18:06



















                    • if helped, then accept the answer..

                      – Awais Tariq
                      Nov 10 '11 at 11:48











                    • ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)

                      – Awais Tariq
                      Nov 10 '11 at 12:51








                    • 2





                      what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..

                      – Awais Tariq
                      Nov 10 '11 at 17:38






                    • 13





                      This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.

                      – lilbyrdie
                      May 20 '14 at 15:19






                    • 13





                      This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would be android:launchMode = "singleTop", which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use the singleInstance option.

                      – Nohus
                      May 16 '17 at 18:06

















                    if helped, then accept the answer..

                    – Awais Tariq
                    Nov 10 '11 at 11:48





                    if helped, then accept the answer..

                    – Awais Tariq
                    Nov 10 '11 at 11:48













                    ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)

                    – Awais Tariq
                    Nov 10 '11 at 12:51







                    ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)

                    – Awais Tariq
                    Nov 10 '11 at 12:51






                    2




                    2





                    what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..

                    – Awais Tariq
                    Nov 10 '11 at 17:38





                    what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..

                    – Awais Tariq
                    Nov 10 '11 at 17:38




                    13




                    13





                    This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.

                    – lilbyrdie
                    May 20 '14 at 15:19





                    This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.

                    – lilbyrdie
                    May 20 '14 at 15:19




                    13




                    13





                    This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would be android:launchMode = "singleTop", which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use the singleInstance option.

                    – Nohus
                    May 16 '17 at 18:06





                    This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would be android:launchMode = "singleTop", which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use the singleInstance option.

                    – Nohus
                    May 16 '17 at 18:06











                    30














                    You can use the intent flags like this.



                    Intent intent = new Intent(Class.class);    
                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    activity.startActivity(intent);


                    It will make only one activity be open at the top of the history stack.






                    share|improve this answer





















                    • 2





                      this should be the correct android-way answer

                      – rocketspacer
                      May 16 '16 at 4:26








                    • 3





                      This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest: android:launchMode = "singleTop", this way it's solved without having to add the flag to every Intent.

                      – Nohus
                      May 16 '17 at 18:09
















                    30














                    You can use the intent flags like this.



                    Intent intent = new Intent(Class.class);    
                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    activity.startActivity(intent);


                    It will make only one activity be open at the top of the history stack.






                    share|improve this answer





















                    • 2





                      this should be the correct android-way answer

                      – rocketspacer
                      May 16 '16 at 4:26








                    • 3





                      This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest: android:launchMode = "singleTop", this way it's solved without having to add the flag to every Intent.

                      – Nohus
                      May 16 '17 at 18:09














                    30












                    30








                    30







                    You can use the intent flags like this.



                    Intent intent = new Intent(Class.class);    
                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    activity.startActivity(intent);


                    It will make only one activity be open at the top of the history stack.






                    share|improve this answer















                    You can use the intent flags like this.



                    Intent intent = new Intent(Class.class);    
                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    activity.startActivity(intent);


                    It will make only one activity be open at the top of the history stack.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 3 '17 at 22:45









                    Darush

                    4,43033042




                    4,43033042










                    answered Feb 20 '14 at 11:47









                    Carlos EduardoLCarlos EduardoL

                    487414




                    487414








                    • 2





                      this should be the correct android-way answer

                      – rocketspacer
                      May 16 '16 at 4:26








                    • 3





                      This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest: android:launchMode = "singleTop", this way it's solved without having to add the flag to every Intent.

                      – Nohus
                      May 16 '17 at 18:09














                    • 2





                      this should be the correct android-way answer

                      – rocketspacer
                      May 16 '16 at 4:26








                    • 3





                      This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest: android:launchMode = "singleTop", this way it's solved without having to add the flag to every Intent.

                      – Nohus
                      May 16 '17 at 18:09








                    2




                    2





                    this should be the correct android-way answer

                    – rocketspacer
                    May 16 '16 at 4:26







                    this should be the correct android-way answer

                    – rocketspacer
                    May 16 '16 at 4:26






                    3




                    3





                    This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest: android:launchMode = "singleTop", this way it's solved without having to add the flag to every Intent.

                    – Nohus
                    May 16 '17 at 18:09





                    This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest: android:launchMode = "singleTop", this way it's solved without having to add the flag to every Intent.

                    – Nohus
                    May 16 '17 at 18:09











                    11














                    Since SO doesn't allow me to comment on other answers, I have to pollute this thread with a new answer.



                    Common answers for the "activity opens twice" problem and my experiences with these solutions (Android 7.1.1):




                    1. Disable button that starts the activity: Works but feels a little clumsy. If you have multiple ways to start the activity in your app (e.g. a button in the action bar AND by clicking on an item in a list view), you have to keep track of the enabled/disabled state of multiple GUI elements. Plus it's not very convenient to disable clicked items in a list view, for example. So, not a very universal approach.

                    2. launchMode="singleInstance": Not working with startActivityForResult(), breaks back navigation with startActivity(), not recommended for regular applications by Android manifest documentation.

                    3. launchMode="singleTask": Not working with startActivityForResult(), not recommended for regular applications by Android manifest documentation.

                    4. FLAG_ACTIVITY_REORDER_TO_FRONT: Breaks back button.

                    5. FLAG_ACTIVITY_SINGLE_TOP: Not working, activity is still opened twice.

                    6. FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me.


                    EDIT: This was for starting activities with startActivity(). When using startActivityForResult() I need to set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.






                    share|improve this answer


























                    • FLAG_ACTIVITY_SINGLE_TOP works for me

                      – Alec von Barnekow
                      Jun 6 '18 at 16:37











                    • FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1

                      – Mingjiang Shi
                      Oct 19 '18 at 4:11











                    • I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?

                      – Mirmuhsin Sodiqov
                      Jan 4 at 7:40











                    • I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.

                      – Mirmuhsin Sodiqov
                      Jan 4 at 9:08
















                    11














                    Since SO doesn't allow me to comment on other answers, I have to pollute this thread with a new answer.



                    Common answers for the "activity opens twice" problem and my experiences with these solutions (Android 7.1.1):




                    1. Disable button that starts the activity: Works but feels a little clumsy. If you have multiple ways to start the activity in your app (e.g. a button in the action bar AND by clicking on an item in a list view), you have to keep track of the enabled/disabled state of multiple GUI elements. Plus it's not very convenient to disable clicked items in a list view, for example. So, not a very universal approach.

                    2. launchMode="singleInstance": Not working with startActivityForResult(), breaks back navigation with startActivity(), not recommended for regular applications by Android manifest documentation.

                    3. launchMode="singleTask": Not working with startActivityForResult(), not recommended for regular applications by Android manifest documentation.

                    4. FLAG_ACTIVITY_REORDER_TO_FRONT: Breaks back button.

                    5. FLAG_ACTIVITY_SINGLE_TOP: Not working, activity is still opened twice.

                    6. FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me.


                    EDIT: This was for starting activities with startActivity(). When using startActivityForResult() I need to set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.






                    share|improve this answer


























                    • FLAG_ACTIVITY_SINGLE_TOP works for me

                      – Alec von Barnekow
                      Jun 6 '18 at 16:37











                    • FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1

                      – Mingjiang Shi
                      Oct 19 '18 at 4:11











                    • I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?

                      – Mirmuhsin Sodiqov
                      Jan 4 at 7:40











                    • I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.

                      – Mirmuhsin Sodiqov
                      Jan 4 at 9:08














                    11












                    11








                    11







                    Since SO doesn't allow me to comment on other answers, I have to pollute this thread with a new answer.



                    Common answers for the "activity opens twice" problem and my experiences with these solutions (Android 7.1.1):




                    1. Disable button that starts the activity: Works but feels a little clumsy. If you have multiple ways to start the activity in your app (e.g. a button in the action bar AND by clicking on an item in a list view), you have to keep track of the enabled/disabled state of multiple GUI elements. Plus it's not very convenient to disable clicked items in a list view, for example. So, not a very universal approach.

                    2. launchMode="singleInstance": Not working with startActivityForResult(), breaks back navigation with startActivity(), not recommended for regular applications by Android manifest documentation.

                    3. launchMode="singleTask": Not working with startActivityForResult(), not recommended for regular applications by Android manifest documentation.

                    4. FLAG_ACTIVITY_REORDER_TO_FRONT: Breaks back button.

                    5. FLAG_ACTIVITY_SINGLE_TOP: Not working, activity is still opened twice.

                    6. FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me.


                    EDIT: This was for starting activities with startActivity(). When using startActivityForResult() I need to set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.






                    share|improve this answer















                    Since SO doesn't allow me to comment on other answers, I have to pollute this thread with a new answer.



                    Common answers for the "activity opens twice" problem and my experiences with these solutions (Android 7.1.1):




                    1. Disable button that starts the activity: Works but feels a little clumsy. If you have multiple ways to start the activity in your app (e.g. a button in the action bar AND by clicking on an item in a list view), you have to keep track of the enabled/disabled state of multiple GUI elements. Plus it's not very convenient to disable clicked items in a list view, for example. So, not a very universal approach.

                    2. launchMode="singleInstance": Not working with startActivityForResult(), breaks back navigation with startActivity(), not recommended for regular applications by Android manifest documentation.

                    3. launchMode="singleTask": Not working with startActivityForResult(), not recommended for regular applications by Android manifest documentation.

                    4. FLAG_ACTIVITY_REORDER_TO_FRONT: Breaks back button.

                    5. FLAG_ACTIVITY_SINGLE_TOP: Not working, activity is still opened twice.

                    6. FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me.


                    EDIT: This was for starting activities with startActivity(). When using startActivityForResult() I need to set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jun 21 '17 at 6:07

























                    answered Jun 8 '17 at 10:37









                    Andy RoidAndy Roid

                    12816




                    12816













                    • FLAG_ACTIVITY_SINGLE_TOP works for me

                      – Alec von Barnekow
                      Jun 6 '18 at 16:37











                    • FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1

                      – Mingjiang Shi
                      Oct 19 '18 at 4:11











                    • I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?

                      – Mirmuhsin Sodiqov
                      Jan 4 at 7:40











                    • I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.

                      – Mirmuhsin Sodiqov
                      Jan 4 at 9:08



















                    • FLAG_ACTIVITY_SINGLE_TOP works for me

                      – Alec von Barnekow
                      Jun 6 '18 at 16:37











                    • FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1

                      – Mingjiang Shi
                      Oct 19 '18 at 4:11











                    • I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?

                      – Mirmuhsin Sodiqov
                      Jan 4 at 7:40











                    • I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.

                      – Mirmuhsin Sodiqov
                      Jan 4 at 9:08

















                    FLAG_ACTIVITY_SINGLE_TOP works for me

                    – Alec von Barnekow
                    Jun 6 '18 at 16:37





                    FLAG_ACTIVITY_SINGLE_TOP works for me

                    – Alec von Barnekow
                    Jun 6 '18 at 16:37













                    FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1

                    – Mingjiang Shi
                    Oct 19 '18 at 4:11





                    FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1

                    – Mingjiang Shi
                    Oct 19 '18 at 4:11













                    I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?

                    – Mirmuhsin Sodiqov
                    Jan 4 at 7:40





                    I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?

                    – Mirmuhsin Sodiqov
                    Jan 4 at 7:40













                    I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.

                    – Mirmuhsin Sodiqov
                    Jan 4 at 9:08





                    I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.

                    – Mirmuhsin Sodiqov
                    Jan 4 at 9:08











                    5














                    Let's say @wannik is right but if we have more than 1 button calling same action listener and i click two buttons once almost same time before starting next activity...



                    So it is good if you have field private boolean mIsClicked = false; and in the listener:



                    if(!mIsClicked)
                    {
                    mIsClicked = true;
                    Intent i = new Intent(this, AnotherActitivty.class);
                    startActivity(i);
                    }


                    And onResume() we need to return the state:



                    @Override
                    protected void onResume() {
                    super.onResume();

                    mIsClicked = false;
                    }


                    What's the deifference between my and @wannik's answer?



                    If you set enabled to false in the listener of it's calling view other button using same listener will still be enabled. So to be sure that listener's action is not called twice you need to have something global that disables all callings of the listener(nevermind if it's new instance or no)



                    What is the difference between my answer and others?



                    They are thinking in right way but they are not thinking for future return to the same instance of the calling activity :)






                    share|improve this answer


























                    • servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)

                      – tejas
                      Jun 24 '13 at 8:02








                    • 1





                      I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity

                      – Sir NIkolay Cesar The First
                      Jun 27 '13 at 12:52






                    • 1





                      This isn't sufficient. You need to use a synchronized(mIsClicked) {...} as well to be 100% safe.

                      – Monstieur
                      Dec 20 '13 at 12:59











                    • That's not needed :)

                      – Sir NIkolay Cesar The First
                      Dec 21 '13 at 9:33











                    • @Monstieur you don’t need a synchronized block because this is all Main Thread…

                      – Martin Marconcini
                      Mar 5 '18 at 21:41
















                    5














                    Let's say @wannik is right but if we have more than 1 button calling same action listener and i click two buttons once almost same time before starting next activity...



                    So it is good if you have field private boolean mIsClicked = false; and in the listener:



                    if(!mIsClicked)
                    {
                    mIsClicked = true;
                    Intent i = new Intent(this, AnotherActitivty.class);
                    startActivity(i);
                    }


                    And onResume() we need to return the state:



                    @Override
                    protected void onResume() {
                    super.onResume();

                    mIsClicked = false;
                    }


                    What's the deifference between my and @wannik's answer?



                    If you set enabled to false in the listener of it's calling view other button using same listener will still be enabled. So to be sure that listener's action is not called twice you need to have something global that disables all callings of the listener(nevermind if it's new instance or no)



                    What is the difference between my answer and others?



                    They are thinking in right way but they are not thinking for future return to the same instance of the calling activity :)






                    share|improve this answer


























                    • servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)

                      – tejas
                      Jun 24 '13 at 8:02








                    • 1





                      I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity

                      – Sir NIkolay Cesar The First
                      Jun 27 '13 at 12:52






                    • 1





                      This isn't sufficient. You need to use a synchronized(mIsClicked) {...} as well to be 100% safe.

                      – Monstieur
                      Dec 20 '13 at 12:59











                    • That's not needed :)

                      – Sir NIkolay Cesar The First
                      Dec 21 '13 at 9:33











                    • @Monstieur you don’t need a synchronized block because this is all Main Thread…

                      – Martin Marconcini
                      Mar 5 '18 at 21:41














                    5












                    5








                    5







                    Let's say @wannik is right but if we have more than 1 button calling same action listener and i click two buttons once almost same time before starting next activity...



                    So it is good if you have field private boolean mIsClicked = false; and in the listener:



                    if(!mIsClicked)
                    {
                    mIsClicked = true;
                    Intent i = new Intent(this, AnotherActitivty.class);
                    startActivity(i);
                    }


                    And onResume() we need to return the state:



                    @Override
                    protected void onResume() {
                    super.onResume();

                    mIsClicked = false;
                    }


                    What's the deifference between my and @wannik's answer?



                    If you set enabled to false in the listener of it's calling view other button using same listener will still be enabled. So to be sure that listener's action is not called twice you need to have something global that disables all callings of the listener(nevermind if it's new instance or no)



                    What is the difference between my answer and others?



                    They are thinking in right way but they are not thinking for future return to the same instance of the calling activity :)






                    share|improve this answer















                    Let's say @wannik is right but if we have more than 1 button calling same action listener and i click two buttons once almost same time before starting next activity...



                    So it is good if you have field private boolean mIsClicked = false; and in the listener:



                    if(!mIsClicked)
                    {
                    mIsClicked = true;
                    Intent i = new Intent(this, AnotherActitivty.class);
                    startActivity(i);
                    }


                    And onResume() we need to return the state:



                    @Override
                    protected void onResume() {
                    super.onResume();

                    mIsClicked = false;
                    }


                    What's the deifference between my and @wannik's answer?



                    If you set enabled to false in the listener of it's calling view other button using same listener will still be enabled. So to be sure that listener's action is not called twice you need to have something global that disables all callings of the listener(nevermind if it's new instance or no)



                    What is the difference between my answer and others?



                    They are thinking in right way but they are not thinking for future return to the same instance of the calling activity :)







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jun 24 '13 at 7:49

























                    answered Jun 24 '13 at 7:43









                    Sir NIkolay Cesar The FirstSir NIkolay Cesar The First

                    784719




                    784719













                    • servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)

                      – tejas
                      Jun 24 '13 at 8:02








                    • 1





                      I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity

                      – Sir NIkolay Cesar The First
                      Jun 27 '13 at 12:52






                    • 1





                      This isn't sufficient. You need to use a synchronized(mIsClicked) {...} as well to be 100% safe.

                      – Monstieur
                      Dec 20 '13 at 12:59











                    • That's not needed :)

                      – Sir NIkolay Cesar The First
                      Dec 21 '13 at 9:33











                    • @Monstieur you don’t need a synchronized block because this is all Main Thread…

                      – Martin Marconcini
                      Mar 5 '18 at 21:41



















                    • servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)

                      – tejas
                      Jun 24 '13 at 8:02








                    • 1





                      I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity

                      – Sir NIkolay Cesar The First
                      Jun 27 '13 at 12:52






                    • 1





                      This isn't sufficient. You need to use a synchronized(mIsClicked) {...} as well to be 100% safe.

                      – Monstieur
                      Dec 20 '13 at 12:59











                    • That's not needed :)

                      – Sir NIkolay Cesar The First
                      Dec 21 '13 at 9:33











                    • @Monstieur you don’t need a synchronized block because this is all Main Thread…

                      – Martin Marconcini
                      Mar 5 '18 at 21:41

















                    servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)

                    – tejas
                    Jun 24 '13 at 8:02







                    servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)

                    – tejas
                    Jun 24 '13 at 8:02






                    1




                    1





                    I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity

                    – Sir NIkolay Cesar The First
                    Jun 27 '13 at 12:52





                    I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity

                    – Sir NIkolay Cesar The First
                    Jun 27 '13 at 12:52




                    1




                    1





                    This isn't sufficient. You need to use a synchronized(mIsClicked) {...} as well to be 100% safe.

                    – Monstieur
                    Dec 20 '13 at 12:59





                    This isn't sufficient. You need to use a synchronized(mIsClicked) {...} as well to be 100% safe.

                    – Monstieur
                    Dec 20 '13 at 12:59













                    That's not needed :)

                    – Sir NIkolay Cesar The First
                    Dec 21 '13 at 9:33





                    That's not needed :)

                    – Sir NIkolay Cesar The First
                    Dec 21 '13 at 9:33













                    @Monstieur you don’t need a synchronized block because this is all Main Thread…

                    – Martin Marconcini
                    Mar 5 '18 at 21:41





                    @Monstieur you don’t need a synchronized block because this is all Main Thread…

                    – Martin Marconcini
                    Mar 5 '18 at 21:41











                    4














                    Use singleInstance to avoid activity to invoke twice.



                    <activity
                    android:name=".MainActivity"
                    android:label="@string/activity"
                    android:launchMode = "singleInstance" />





                    share|improve this answer




























                      4














                      Use singleInstance to avoid activity to invoke twice.



                      <activity
                      android:name=".MainActivity"
                      android:label="@string/activity"
                      android:launchMode = "singleInstance" />





                      share|improve this answer


























                        4












                        4








                        4







                        Use singleInstance to avoid activity to invoke twice.



                        <activity
                        android:name=".MainActivity"
                        android:label="@string/activity"
                        android:launchMode = "singleInstance" />





                        share|improve this answer













                        Use singleInstance to avoid activity to invoke twice.



                        <activity
                        android:name=".MainActivity"
                        android:label="@string/activity"
                        android:launchMode = "singleInstance" />






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jun 3 '16 at 13:44









                        Manvendra PriyadarshiManvendra Priyadarshi

                        15715




                        15715























                            3














                            I think you're going about solving the problem the wrong way. Generally it is a bad idea for an activity to be making long-running web requests in any of its startup lifecycle methods (onCreate(), onResume(), etc). Really these methods should simply be used to instantiate and initialise objects your activity will use and should therefore be relatively quick.



                            If you need to be performing a web request then do this in a background thread from your newly launched activity (and show the loading dialog in the new activity). Once the background request thread completes it can update the activity and hide the dialog.



                            This then means your new activity should get launched immediately and prevent the double click from being possible.






                            share|improve this answer




























                              3














                              I think you're going about solving the problem the wrong way. Generally it is a bad idea for an activity to be making long-running web requests in any of its startup lifecycle methods (onCreate(), onResume(), etc). Really these methods should simply be used to instantiate and initialise objects your activity will use and should therefore be relatively quick.



                              If you need to be performing a web request then do this in a background thread from your newly launched activity (and show the loading dialog in the new activity). Once the background request thread completes it can update the activity and hide the dialog.



                              This then means your new activity should get launched immediately and prevent the double click from being possible.






                              share|improve this answer


























                                3












                                3








                                3







                                I think you're going about solving the problem the wrong way. Generally it is a bad idea for an activity to be making long-running web requests in any of its startup lifecycle methods (onCreate(), onResume(), etc). Really these methods should simply be used to instantiate and initialise objects your activity will use and should therefore be relatively quick.



                                If you need to be performing a web request then do this in a background thread from your newly launched activity (and show the loading dialog in the new activity). Once the background request thread completes it can update the activity and hide the dialog.



                                This then means your new activity should get launched immediately and prevent the double click from being possible.






                                share|improve this answer













                                I think you're going about solving the problem the wrong way. Generally it is a bad idea for an activity to be making long-running web requests in any of its startup lifecycle methods (onCreate(), onResume(), etc). Really these methods should simply be used to instantiate and initialise objects your activity will use and should therefore be relatively quick.



                                If you need to be performing a web request then do this in a background thread from your newly launched activity (and show the loading dialog in the new activity). Once the background request thread completes it can update the activity and hide the dialog.



                                This then means your new activity should get launched immediately and prevent the double click from being possible.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 10 '11 at 10:07









                                tomtheguvnortomtheguvnor

                                2,63812033




                                2,63812033























                                    3














                                    Hope this helps:



                                     protected static final int DELAY_TIME = 100;

                                    // to prevent double click issue, disable button after click and enable it after 100ms
                                    protected Handler mClickHandler = new Handler() {

                                    public void handleMessage(Message msg) {

                                    findViewById(msg.what).setClickable(true);
                                    super.handleMessage(msg);
                                    }
                                    };

                                    @Override
                                    public void onClick(View v) {
                                    int id = v.getId();
                                    v.setClickable(false);
                                    mClickHandler.sendEmptyMessageDelayed(id, DELAY_TIME);
                                    // startActivity()
                                    }`





                                    share|improve this answer



















                                    • 4





                                      lol - what if I click faster then 10x/sec? :D

                                      – Srneczek
                                      Oct 9 '15 at 14:19


















                                    3














                                    Hope this helps:



                                     protected static final int DELAY_TIME = 100;

                                    // to prevent double click issue, disable button after click and enable it after 100ms
                                    protected Handler mClickHandler = new Handler() {

                                    public void handleMessage(Message msg) {

                                    findViewById(msg.what).setClickable(true);
                                    super.handleMessage(msg);
                                    }
                                    };

                                    @Override
                                    public void onClick(View v) {
                                    int id = v.getId();
                                    v.setClickable(false);
                                    mClickHandler.sendEmptyMessageDelayed(id, DELAY_TIME);
                                    // startActivity()
                                    }`





                                    share|improve this answer



















                                    • 4





                                      lol - what if I click faster then 10x/sec? :D

                                      – Srneczek
                                      Oct 9 '15 at 14:19
















                                    3












                                    3








                                    3







                                    Hope this helps:



                                     protected static final int DELAY_TIME = 100;

                                    // to prevent double click issue, disable button after click and enable it after 100ms
                                    protected Handler mClickHandler = new Handler() {

                                    public void handleMessage(Message msg) {

                                    findViewById(msg.what).setClickable(true);
                                    super.handleMessage(msg);
                                    }
                                    };

                                    @Override
                                    public void onClick(View v) {
                                    int id = v.getId();
                                    v.setClickable(false);
                                    mClickHandler.sendEmptyMessageDelayed(id, DELAY_TIME);
                                    // startActivity()
                                    }`





                                    share|improve this answer













                                    Hope this helps:



                                     protected static final int DELAY_TIME = 100;

                                    // to prevent double click issue, disable button after click and enable it after 100ms
                                    protected Handler mClickHandler = new Handler() {

                                    public void handleMessage(Message msg) {

                                    findViewById(msg.what).setClickable(true);
                                    super.handleMessage(msg);
                                    }
                                    };

                                    @Override
                                    public void onClick(View v) {
                                    int id = v.getId();
                                    v.setClickable(false);
                                    mClickHandler.sendEmptyMessageDelayed(id, DELAY_TIME);
                                    // startActivity()
                                    }`






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered May 23 '12 at 7:38









                                    thanhbinh84thanhbinh84

                                    11.8k33746




                                    11.8k33746








                                    • 4





                                      lol - what if I click faster then 10x/sec? :D

                                      – Srneczek
                                      Oct 9 '15 at 14:19
















                                    • 4





                                      lol - what if I click faster then 10x/sec? :D

                                      – Srneczek
                                      Oct 9 '15 at 14:19










                                    4




                                    4





                                    lol - what if I click faster then 10x/sec? :D

                                    – Srneczek
                                    Oct 9 '15 at 14:19







                                    lol - what if I click faster then 10x/sec? :D

                                    – Srneczek
                                    Oct 9 '15 at 14:19













                                    3














                                    It was only working for me when startActivity(intent)



                                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);





                                    share|improve this answer




























                                      3














                                      It was only working for me when startActivity(intent)



                                      intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);





                                      share|improve this answer


























                                        3












                                        3








                                        3







                                        It was only working for me when startActivity(intent)



                                        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);





                                        share|improve this answer













                                        It was only working for me when startActivity(intent)



                                        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Oct 25 '17 at 6:49









                                        Shylendra MaddaShylendra Madda

                                        11.4k64783




                                        11.4k64783























                                            2














                                            For this situation, I'll go for one of two approached, singleTask in manifest.xml OR a flag in the Activity's onResume() & onDestroy() methods respectively.



                                            For the first solution: I prefer to use singleTask for the activity in the manifest rather than singleInstance, as per using singleInstance I figured out that in some occasions the activity creating a new separate instance for itself which result to have a two separate applications window in the running apps in bcakground and besides extra memory allocations that would result a very bad User Experience when the user opens the apps view to choose some app to resume.
                                            So, the better way is to have the activity defined at the manifest.xml like the following:



                                            <activity
                                            android:name=".MainActivity"
                                            android:launchMode="singleTask"</activity>


                                            you can check activity launch modes here.





                                            For the second solution, you have to just define a static variable or a preference variable, for example:



                                            public class MainActivity extends Activity{
                                            public static boolean isRunning = false;

                                            @Override
                                            public void onResume() {
                                            super.onResume();
                                            // now the activity is running
                                            isRunning = true;
                                            }

                                            @Override
                                            public void onDestroy() {
                                            super.onDestroy();
                                            // now the activity will be available again
                                            isRunning = false;
                                            }

                                            }


                                            and from the other side when you want to launch this activity, just check:



                                            private void launchMainActivity(){
                                            if(MainActivity.isRunning)
                                            return;
                                            Intent intent = new Intent(ThisActivity.this, MainActivity.class);
                                            startActivity(intent);
                                            }





                                            share|improve this answer




























                                              2














                                              For this situation, I'll go for one of two approached, singleTask in manifest.xml OR a flag in the Activity's onResume() & onDestroy() methods respectively.



                                              For the first solution: I prefer to use singleTask for the activity in the manifest rather than singleInstance, as per using singleInstance I figured out that in some occasions the activity creating a new separate instance for itself which result to have a two separate applications window in the running apps in bcakground and besides extra memory allocations that would result a very bad User Experience when the user opens the apps view to choose some app to resume.
                                              So, the better way is to have the activity defined at the manifest.xml like the following:



                                              <activity
                                              android:name=".MainActivity"
                                              android:launchMode="singleTask"</activity>


                                              you can check activity launch modes here.





                                              For the second solution, you have to just define a static variable or a preference variable, for example:



                                              public class MainActivity extends Activity{
                                              public static boolean isRunning = false;

                                              @Override
                                              public void onResume() {
                                              super.onResume();
                                              // now the activity is running
                                              isRunning = true;
                                              }

                                              @Override
                                              public void onDestroy() {
                                              super.onDestroy();
                                              // now the activity will be available again
                                              isRunning = false;
                                              }

                                              }


                                              and from the other side when you want to launch this activity, just check:



                                              private void launchMainActivity(){
                                              if(MainActivity.isRunning)
                                              return;
                                              Intent intent = new Intent(ThisActivity.this, MainActivity.class);
                                              startActivity(intent);
                                              }





                                              share|improve this answer


























                                                2












                                                2








                                                2







                                                For this situation, I'll go for one of two approached, singleTask in manifest.xml OR a flag in the Activity's onResume() & onDestroy() methods respectively.



                                                For the first solution: I prefer to use singleTask for the activity in the manifest rather than singleInstance, as per using singleInstance I figured out that in some occasions the activity creating a new separate instance for itself which result to have a two separate applications window in the running apps in bcakground and besides extra memory allocations that would result a very bad User Experience when the user opens the apps view to choose some app to resume.
                                                So, the better way is to have the activity defined at the manifest.xml like the following:



                                                <activity
                                                android:name=".MainActivity"
                                                android:launchMode="singleTask"</activity>


                                                you can check activity launch modes here.





                                                For the second solution, you have to just define a static variable or a preference variable, for example:



                                                public class MainActivity extends Activity{
                                                public static boolean isRunning = false;

                                                @Override
                                                public void onResume() {
                                                super.onResume();
                                                // now the activity is running
                                                isRunning = true;
                                                }

                                                @Override
                                                public void onDestroy() {
                                                super.onDestroy();
                                                // now the activity will be available again
                                                isRunning = false;
                                                }

                                                }


                                                and from the other side when you want to launch this activity, just check:



                                                private void launchMainActivity(){
                                                if(MainActivity.isRunning)
                                                return;
                                                Intent intent = new Intent(ThisActivity.this, MainActivity.class);
                                                startActivity(intent);
                                                }





                                                share|improve this answer













                                                For this situation, I'll go for one of two approached, singleTask in manifest.xml OR a flag in the Activity's onResume() & onDestroy() methods respectively.



                                                For the first solution: I prefer to use singleTask for the activity in the manifest rather than singleInstance, as per using singleInstance I figured out that in some occasions the activity creating a new separate instance for itself which result to have a two separate applications window in the running apps in bcakground and besides extra memory allocations that would result a very bad User Experience when the user opens the apps view to choose some app to resume.
                                                So, the better way is to have the activity defined at the manifest.xml like the following:



                                                <activity
                                                android:name=".MainActivity"
                                                android:launchMode="singleTask"</activity>


                                                you can check activity launch modes here.





                                                For the second solution, you have to just define a static variable or a preference variable, for example:



                                                public class MainActivity extends Activity{
                                                public static boolean isRunning = false;

                                                @Override
                                                public void onResume() {
                                                super.onResume();
                                                // now the activity is running
                                                isRunning = true;
                                                }

                                                @Override
                                                public void onDestroy() {
                                                super.onDestroy();
                                                // now the activity will be available again
                                                isRunning = false;
                                                }

                                                }


                                                and from the other side when you want to launch this activity, just check:



                                                private void launchMainActivity(){
                                                if(MainActivity.isRunning)
                                                return;
                                                Intent intent = new Intent(ThisActivity.this, MainActivity.class);
                                                startActivity(intent);
                                                }






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Apr 9 '17 at 11:38









                                                Muhammed RefaatMuhammed Refaat

                                                5,96296092




                                                5,96296092























                                                    1














                                                    Other very very simple solution if you no want use onActivityResult() is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:



                                                       final Button btn = ...
                                                    btn.setOnClickListener(new OnClickListener() {
                                                    public void onClick(View v) {
                                                    //start activity here...
                                                    btn.setEnabled(false); //disable button

                                                    //post a message to run in UI Thread after a delay in milliseconds
                                                    btn.postDelayed(new Runnable() {
                                                    public void run() {
                                                    btn.setEnabled(true); //enable button again
                                                    }
                                                    },1000); //1 second in this case...
                                                    }
                                                    });





                                                    share|improve this answer




























                                                      1














                                                      Other very very simple solution if you no want use onActivityResult() is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:



                                                         final Button btn = ...
                                                      btn.setOnClickListener(new OnClickListener() {
                                                      public void onClick(View v) {
                                                      //start activity here...
                                                      btn.setEnabled(false); //disable button

                                                      //post a message to run in UI Thread after a delay in milliseconds
                                                      btn.postDelayed(new Runnable() {
                                                      public void run() {
                                                      btn.setEnabled(true); //enable button again
                                                      }
                                                      },1000); //1 second in this case...
                                                      }
                                                      });





                                                      share|improve this answer


























                                                        1












                                                        1








                                                        1







                                                        Other very very simple solution if you no want use onActivityResult() is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:



                                                           final Button btn = ...
                                                        btn.setOnClickListener(new OnClickListener() {
                                                        public void onClick(View v) {
                                                        //start activity here...
                                                        btn.setEnabled(false); //disable button

                                                        //post a message to run in UI Thread after a delay in milliseconds
                                                        btn.postDelayed(new Runnable() {
                                                        public void run() {
                                                        btn.setEnabled(true); //enable button again
                                                        }
                                                        },1000); //1 second in this case...
                                                        }
                                                        });





                                                        share|improve this answer













                                                        Other very very simple solution if you no want use onActivityResult() is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:



                                                           final Button btn = ...
                                                        btn.setOnClickListener(new OnClickListener() {
                                                        public void onClick(View v) {
                                                        //start activity here...
                                                        btn.setEnabled(false); //disable button

                                                        //post a message to run in UI Thread after a delay in milliseconds
                                                        btn.postDelayed(new Runnable() {
                                                        public void run() {
                                                        btn.setEnabled(true); //enable button again
                                                        }
                                                        },1000); //1 second in this case...
                                                        }
                                                        });






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Jun 18 '16 at 18:54









                                                        GilianGilian

                                                        227511




                                                        227511























                                                            0














                                                            Just maintain one flag in button onClick method as:



                                                            public boolean oneTimeLoadActivity = false;



                                                                myButton.setOnClickListener(new View.OnClickListener() {
                                                            public void onClick(View view) {
                                                            if(!oneTimeLoadActivity){
                                                            //start your new activity.
                                                            oneTimeLoadActivity = true;
                                                            }
                                                            }
                                                            });





                                                            share|improve this answer




























                                                              0














                                                              Just maintain one flag in button onClick method as:



                                                              public boolean oneTimeLoadActivity = false;



                                                                  myButton.setOnClickListener(new View.OnClickListener() {
                                                              public void onClick(View view) {
                                                              if(!oneTimeLoadActivity){
                                                              //start your new activity.
                                                              oneTimeLoadActivity = true;
                                                              }
                                                              }
                                                              });





                                                              share|improve this answer


























                                                                0












                                                                0








                                                                0







                                                                Just maintain one flag in button onClick method as:



                                                                public boolean oneTimeLoadActivity = false;



                                                                    myButton.setOnClickListener(new View.OnClickListener() {
                                                                public void onClick(View view) {
                                                                if(!oneTimeLoadActivity){
                                                                //start your new activity.
                                                                oneTimeLoadActivity = true;
                                                                }
                                                                }
                                                                });





                                                                share|improve this answer













                                                                Just maintain one flag in button onClick method as:



                                                                public boolean oneTimeLoadActivity = false;



                                                                    myButton.setOnClickListener(new View.OnClickListener() {
                                                                public void onClick(View view) {
                                                                if(!oneTimeLoadActivity){
                                                                //start your new activity.
                                                                oneTimeLoadActivity = true;
                                                                }
                                                                }
                                                                });






                                                                share|improve this answer












                                                                share|improve this answer



                                                                share|improve this answer










                                                                answered Nov 10 '11 at 10:05









                                                                Balaji KhadakeBalaji Khadake

                                                                2,78342035




                                                                2,78342035























                                                                    0














                                                                    If you're using onActivityResult, you could use a variable to save state.



                                                                    private Boolean activityOpenInProgress = false;

                                                                    myButton.setOnClickListener(new View.OnClickListener() {
                                                                    public void onClick(View view) {
                                                                    if( activityOpenInProgress )
                                                                    return;

                                                                    activityOpenInProgress = true;
                                                                    //Load another activity with startActivityForResult with required request code
                                                                    }
                                                                    });

                                                                    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                                                                    if( requestCode == thatYouSentToOpenActivity ){
                                                                    activityOpenInProgress = false;
                                                                    }
                                                                    }


                                                                    Works on back button pressed too because request code is returned on event.






                                                                    share|improve this answer




























                                                                      0














                                                                      If you're using onActivityResult, you could use a variable to save state.



                                                                      private Boolean activityOpenInProgress = false;

                                                                      myButton.setOnClickListener(new View.OnClickListener() {
                                                                      public void onClick(View view) {
                                                                      if( activityOpenInProgress )
                                                                      return;

                                                                      activityOpenInProgress = true;
                                                                      //Load another activity with startActivityForResult with required request code
                                                                      }
                                                                      });

                                                                      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                                                                      if( requestCode == thatYouSentToOpenActivity ){
                                                                      activityOpenInProgress = false;
                                                                      }
                                                                      }


                                                                      Works on back button pressed too because request code is returned on event.






                                                                      share|improve this answer


























                                                                        0












                                                                        0








                                                                        0







                                                                        If you're using onActivityResult, you could use a variable to save state.



                                                                        private Boolean activityOpenInProgress = false;

                                                                        myButton.setOnClickListener(new View.OnClickListener() {
                                                                        public void onClick(View view) {
                                                                        if( activityOpenInProgress )
                                                                        return;

                                                                        activityOpenInProgress = true;
                                                                        //Load another activity with startActivityForResult with required request code
                                                                        }
                                                                        });

                                                                        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                                                                        if( requestCode == thatYouSentToOpenActivity ){
                                                                        activityOpenInProgress = false;
                                                                        }
                                                                        }


                                                                        Works on back button pressed too because request code is returned on event.






                                                                        share|improve this answer













                                                                        If you're using onActivityResult, you could use a variable to save state.



                                                                        private Boolean activityOpenInProgress = false;

                                                                        myButton.setOnClickListener(new View.OnClickListener() {
                                                                        public void onClick(View view) {
                                                                        if( activityOpenInProgress )
                                                                        return;

                                                                        activityOpenInProgress = true;
                                                                        //Load another activity with startActivityForResult with required request code
                                                                        }
                                                                        });

                                                                        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                                                                        if( requestCode == thatYouSentToOpenActivity ){
                                                                        activityOpenInProgress = false;
                                                                        }
                                                                        }


                                                                        Works on back button pressed too because request code is returned on event.







                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Oct 30 '15 at 22:01









                                                                        UmangUmang

                                                                        196111




                                                                        196111























                                                                            0














                                                                            Adding:



                                                                            android:launchMode="singleTop"


                                                                            Inside the activity tag in AndroidManifest.xml solved the issue.






                                                                            share|improve this answer




























                                                                              0














                                                                              Adding:



                                                                              android:launchMode="singleTop"


                                                                              Inside the activity tag in AndroidManifest.xml solved the issue.






                                                                              share|improve this answer


























                                                                                0












                                                                                0








                                                                                0







                                                                                Adding:



                                                                                android:launchMode="singleTop"


                                                                                Inside the activity tag in AndroidManifest.xml solved the issue.






                                                                                share|improve this answer













                                                                                Adding:



                                                                                android:launchMode="singleTop"


                                                                                Inside the activity tag in AndroidManifest.xml solved the issue.







                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered Oct 8 '18 at 12:42









                                                                                Fabio TrottaFabio Trotta

                                                                                4616




                                                                                4616























                                                                                    -1














                                                                                    myButton.setOnClickListener(new View.OnClickListener() {
                                                                                    public void onClick(View view) {
                                                                                    myButton.setOnClickListener(null);
                                                                                    }
                                                                                    });





                                                                                    share|improve this answer
























                                                                                    • That would probably not work since you would have to declare it as final.

                                                                                      – King
                                                                                      Mar 29 '15 at 11:10
















                                                                                    -1














                                                                                    myButton.setOnClickListener(new View.OnClickListener() {
                                                                                    public void onClick(View view) {
                                                                                    myButton.setOnClickListener(null);
                                                                                    }
                                                                                    });





                                                                                    share|improve this answer
























                                                                                    • That would probably not work since you would have to declare it as final.

                                                                                      – King
                                                                                      Mar 29 '15 at 11:10














                                                                                    -1












                                                                                    -1








                                                                                    -1







                                                                                    myButton.setOnClickListener(new View.OnClickListener() {
                                                                                    public void onClick(View view) {
                                                                                    myButton.setOnClickListener(null);
                                                                                    }
                                                                                    });





                                                                                    share|improve this answer













                                                                                    myButton.setOnClickListener(new View.OnClickListener() {
                                                                                    public void onClick(View view) {
                                                                                    myButton.setOnClickListener(null);
                                                                                    }
                                                                                    });






                                                                                    share|improve this answer












                                                                                    share|improve this answer



                                                                                    share|improve this answer










                                                                                    answered Nov 10 '11 at 10:04









                                                                                    Thunder RabbitThunder Rabbit

                                                                                    3,32953777




                                                                                    3,32953777













                                                                                    • That would probably not work since you would have to declare it as final.

                                                                                      – King
                                                                                      Mar 29 '15 at 11:10



















                                                                                    • That would probably not work since you would have to declare it as final.

                                                                                      – King
                                                                                      Mar 29 '15 at 11:10

















                                                                                    That would probably not work since you would have to declare it as final.

                                                                                    – King
                                                                                    Mar 29 '15 at 11:10





                                                                                    That would probably not work since you would have to declare it as final.

                                                                                    – King
                                                                                    Mar 29 '15 at 11:10











                                                                                    -1














                                                                                    Use a flag variable set it to true,
                                                                                    Check if its true just return else perform Activity Call.



                                                                                    You can also use setClickable(false) one executing the Activity Call



                                                                                    flg=false
                                                                                    public void onClick(View view) {
                                                                                    if(flg==true)
                                                                                    return;
                                                                                    else
                                                                                    { flg=true;
                                                                                    // perform click}
                                                                                    }





                                                                                    share|improve this answer
























                                                                                    • perform click; wait; flg = false; for when we get back

                                                                                      – Xeno Lupus
                                                                                      Nov 10 '11 at 11:29


















                                                                                    -1














                                                                                    Use a flag variable set it to true,
                                                                                    Check if its true just return else perform Activity Call.



                                                                                    You can also use setClickable(false) one executing the Activity Call



                                                                                    flg=false
                                                                                    public void onClick(View view) {
                                                                                    if(flg==true)
                                                                                    return;
                                                                                    else
                                                                                    { flg=true;
                                                                                    // perform click}
                                                                                    }





                                                                                    share|improve this answer
























                                                                                    • perform click; wait; flg = false; for when we get back

                                                                                      – Xeno Lupus
                                                                                      Nov 10 '11 at 11:29
















                                                                                    -1












                                                                                    -1








                                                                                    -1







                                                                                    Use a flag variable set it to true,
                                                                                    Check if its true just return else perform Activity Call.



                                                                                    You can also use setClickable(false) one executing the Activity Call



                                                                                    flg=false
                                                                                    public void onClick(View view) {
                                                                                    if(flg==true)
                                                                                    return;
                                                                                    else
                                                                                    { flg=true;
                                                                                    // perform click}
                                                                                    }





                                                                                    share|improve this answer













                                                                                    Use a flag variable set it to true,
                                                                                    Check if its true just return else perform Activity Call.



                                                                                    You can also use setClickable(false) one executing the Activity Call



                                                                                    flg=false
                                                                                    public void onClick(View view) {
                                                                                    if(flg==true)
                                                                                    return;
                                                                                    else
                                                                                    { flg=true;
                                                                                    // perform click}
                                                                                    }






                                                                                    share|improve this answer












                                                                                    share|improve this answer



                                                                                    share|improve this answer










                                                                                    answered Nov 10 '11 at 10:04









                                                                                    MKJParekhMKJParekh

                                                                                    29.8k107995




                                                                                    29.8k107995













                                                                                    • perform click; wait; flg = false; for when we get back

                                                                                      – Xeno Lupus
                                                                                      Nov 10 '11 at 11:29





















                                                                                    • perform click; wait; flg = false; for when we get back

                                                                                      – Xeno Lupus
                                                                                      Nov 10 '11 at 11:29



















                                                                                    perform click; wait; flg = false; for when we get back

                                                                                    – Xeno Lupus
                                                                                    Nov 10 '11 at 11:29







                                                                                    perform click; wait; flg = false; for when we get back

                                                                                    – Xeno Lupus
                                                                                    Nov 10 '11 at 11:29













                                                                                    -2














                                                                                    You can try this also



                                                                                    Button game = (Button) findViewById(R.id.games);
                                                                                    game.setOnClickListener(new View.OnClickListener()
                                                                                    {
                                                                                    public void onClick(View view)
                                                                                    {
                                                                                    Intent myIntent = new Intent(view.getContext(), Games.class);
                                                                                    startActivityForResult(myIntent, 0);
                                                                                    }

                                                                                    });





                                                                                    share|improve this answer
























                                                                                    • This does not work

                                                                                      – tejas
                                                                                      Nov 10 '11 at 12:48
















                                                                                    -2














                                                                                    You can try this also



                                                                                    Button game = (Button) findViewById(R.id.games);
                                                                                    game.setOnClickListener(new View.OnClickListener()
                                                                                    {
                                                                                    public void onClick(View view)
                                                                                    {
                                                                                    Intent myIntent = new Intent(view.getContext(), Games.class);
                                                                                    startActivityForResult(myIntent, 0);
                                                                                    }

                                                                                    });





                                                                                    share|improve this answer
























                                                                                    • This does not work

                                                                                      – tejas
                                                                                      Nov 10 '11 at 12:48














                                                                                    -2












                                                                                    -2








                                                                                    -2







                                                                                    You can try this also



                                                                                    Button game = (Button) findViewById(R.id.games);
                                                                                    game.setOnClickListener(new View.OnClickListener()
                                                                                    {
                                                                                    public void onClick(View view)
                                                                                    {
                                                                                    Intent myIntent = new Intent(view.getContext(), Games.class);
                                                                                    startActivityForResult(myIntent, 0);
                                                                                    }

                                                                                    });





                                                                                    share|improve this answer













                                                                                    You can try this also



                                                                                    Button game = (Button) findViewById(R.id.games);
                                                                                    game.setOnClickListener(new View.OnClickListener()
                                                                                    {
                                                                                    public void onClick(View view)
                                                                                    {
                                                                                    Intent myIntent = new Intent(view.getContext(), Games.class);
                                                                                    startActivityForResult(myIntent, 0);
                                                                                    }

                                                                                    });






                                                                                    share|improve this answer












                                                                                    share|improve this answer



                                                                                    share|improve this answer










                                                                                    answered Nov 10 '11 at 10:48









                                                                                    KarthikKarthik

                                                                                    2,504184480




                                                                                    2,504184480













                                                                                    • This does not work

                                                                                      – tejas
                                                                                      Nov 10 '11 at 12:48



















                                                                                    • This does not work

                                                                                      – tejas
                                                                                      Nov 10 '11 at 12:48

















                                                                                    This does not work

                                                                                    – tejas
                                                                                    Nov 10 '11 at 12:48





                                                                                    This does not work

                                                                                    – tejas
                                                                                    Nov 10 '11 at 12:48











                                                                                    -2














                                                                                    You could just override startActivityForResult and use instance variable:



                                                                                    boolean couldStartActivity = false;

                                                                                    @Override
                                                                                    protected void onResume() {
                                                                                    super.onResume();

                                                                                    couldStartActivity = true;
                                                                                    }

                                                                                    @Override
                                                                                    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
                                                                                    if (couldStartActivity) {
                                                                                    couldStartActivity = false;
                                                                                    intent.putExtra(RequestCodeKey, requestCode);
                                                                                    super.startActivityForResult(intent, requestCode, options);
                                                                                    }
                                                                                    }





                                                                                    share|improve this answer




























                                                                                      -2














                                                                                      You could just override startActivityForResult and use instance variable:



                                                                                      boolean couldStartActivity = false;

                                                                                      @Override
                                                                                      protected void onResume() {
                                                                                      super.onResume();

                                                                                      couldStartActivity = true;
                                                                                      }

                                                                                      @Override
                                                                                      public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
                                                                                      if (couldStartActivity) {
                                                                                      couldStartActivity = false;
                                                                                      intent.putExtra(RequestCodeKey, requestCode);
                                                                                      super.startActivityForResult(intent, requestCode, options);
                                                                                      }
                                                                                      }





                                                                                      share|improve this answer


























                                                                                        -2












                                                                                        -2








                                                                                        -2







                                                                                        You could just override startActivityForResult and use instance variable:



                                                                                        boolean couldStartActivity = false;

                                                                                        @Override
                                                                                        protected void onResume() {
                                                                                        super.onResume();

                                                                                        couldStartActivity = true;
                                                                                        }

                                                                                        @Override
                                                                                        public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
                                                                                        if (couldStartActivity) {
                                                                                        couldStartActivity = false;
                                                                                        intent.putExtra(RequestCodeKey, requestCode);
                                                                                        super.startActivityForResult(intent, requestCode, options);
                                                                                        }
                                                                                        }





                                                                                        share|improve this answer













                                                                                        You could just override startActivityForResult and use instance variable:



                                                                                        boolean couldStartActivity = false;

                                                                                        @Override
                                                                                        protected void onResume() {
                                                                                        super.onResume();

                                                                                        couldStartActivity = true;
                                                                                        }

                                                                                        @Override
                                                                                        public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
                                                                                        if (couldStartActivity) {
                                                                                        couldStartActivity = false;
                                                                                        intent.putExtra(RequestCodeKey, requestCode);
                                                                                        super.startActivityForResult(intent, requestCode, options);
                                                                                        }
                                                                                        }






                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Jul 17 '15 at 17:42









                                                                                        Aleksei MinaevAleksei Minaev

                                                                                        8461015




                                                                                        8461015






























                                                                                            draft saved

                                                                                            draft discarded




















































                                                                                            Thanks for contributing an answer to Stack Overflow!


                                                                                            • Please be sure to answer the question. Provide details and share your research!

                                                                                            But avoid



                                                                                            • Asking for help, clarification, or responding to other answers.

                                                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                                                            To learn more, see our tips on writing great answers.




                                                                                            draft saved


                                                                                            draft discarded














                                                                                            StackExchange.ready(
                                                                                            function () {
                                                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8077728%2fhow-to-prevent-the-activity-from-loading-twice-on-pressing-the-button%23new-answer', 'question_page');
                                                                                            }
                                                                                            );

                                                                                            Post as a guest















                                                                                            Required, but never shown





















































                                                                                            Required, but never shown














                                                                                            Required, but never shown












                                                                                            Required, but never shown







                                                                                            Required, but never shown

































                                                                                            Required, but never shown














                                                                                            Required, but never shown












                                                                                            Required, but never shown







                                                                                            Required, but never shown







                                                                                            Popular posts from this blog

                                                                                            Contact image not getting when fetch all contact list from iPhone by CNContact

                                                                                            count number of partitions of a set with n elements into k subsets

                                                                                            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks