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

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

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

    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.

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

    // variable to track event time

    private long mLastClickTime = 0;
    

    2.In onClick check that if current time and last click time difference are less than i second then dont't do anything(return) else go for click event

     @Override
    public void onClick(View v) {
        // Preventing multiple clicks, using threshold of 1 second
        if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
            return;
              }
        mLastClickTime = SystemClock.elapsedRealtime();
                // Handle button clicks
                if (v == R.id.imageView2) {
            // Do ur stuff.
             }
                else if (v == R.id.imageView2) {
            // Do ur stuff.
             }
          }
     }
    
    0 讨论(0)
  • 2020-12-01 00:29

    Use singleInstance to avoid activity to invoke twice.

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

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

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