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

后端 未结 19 1935
情深已故
情深已故 2020-12-01 00:15

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 butt

相关标签:
19条回答
  • 2020-12-01 00:16

    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.

    0 讨论(0)
  • 2020-12-01 00:17
    myButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
          myButton.setOnClickListener(null);
        }
    });
    
    0 讨论(0)
  • 2020-12-01 00:18

    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}
        } 
    
    0 讨论(0)
  • 2020-12-01 00:20

    add Launch Mode as single task in manifest to avoid activity opening twice on clicking

    <activity
            android:name=".MainActivity"
            android:label="@string/activity"
            android:launchMode = "singleTask" />
    
    0 讨论(0)
  • 2020-12-01 00:22

    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.

    0 讨论(0)
  • 2020-12-01 00:22

    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...
            }
        });
    
    0 讨论(0)
提交回复
热议问题