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

后端 未结 19 1938
情深已故
情深已故 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:34

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

    android:launchMode = "singleTop"
    

    For example:

    <activity
                android:name=".MainActivity"
                android:theme="@style/AppTheme.NoActionBar"
                android:launchMode = "singleTop"/>
    
    0 讨论(0)
  • 2020-12-01 00:36

    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 :)

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

    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.

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

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

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

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