How to Prevent The Activity from Loading Twice on Pressing The Button

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

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

Prevent same activity to be started multiple times

Your problem is two activity started by you button click spamming.

In a simple way set boolean field to prevent starting two activities.

Example:

boolean isClicked;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isClicked){
isClicked = true;
//start activity here.
}
}
});

In your onPause() set the boolean field false.

Activity being created twice on android

The problem is improper handling of the Activity lifecycle.

The second call to onCreate is for handling the result.

Android may choose to destroy an Activity that is waiting for the call to onActivityResult; especially when free memory is running low. Some devices appear more aggressive about destroying Activitys that are on the task stack. I can reliably recreate the issue on a Samsung device set to a debugging mode called "strict mode".

You can verify whether this is your issue by logging calls to onCreate & onDestroy.

In the case of a destroyed activity, when the activity result needs to be processed, Android will recreate the Activity, passing a savedInstanceState to onCreate. So, the remedy is to check the value of savedInstanceState in your GetImageActivity.onCreate. If it is not null then don't make any calls to startActivity because your Activity is being recreated to call onActivityResult.

Optionally, if you need to preserve any state then override onSaveInstanceState(Bundle outState) and put data you need into outState.

How to prevent repeating actions on double click on the button?

This is actually a surprisingly complex problem. The root of the issue is that UI events (e.g. a button click) cause messages to be "posted" to the end of a message queue. If you are fast enough, it's possible to get many of these messages posted to the message queue before the first one is ever processed.

This means that even disabling the button inside your onClick() method won't truly solve the problem (since the disabling won't "happen" until the first message is processed, but you might already have three other duplicate messages waiting in the message queue).

The best thing to do is to track some sort of boolean flag and check the flag every time inside onClick():

private boolean firstClick = true;
button.setOnClickListener(v -> {
if (firstClick) {
firstClick = false;
// do your stuff
}
});

You have to remember to reset firstClick back to true whenever you want to re-enable button taps, of course.

Android activity is running twice

activity launch modes should be used carefully.

android:launchMode="singleTop"

If instance of activity is present on top of Task stack, a new instance will not be created and system will route your intent information through onNewIntent(). If it is not present on top, a new instance will be created. Multiple instance can be created and each instance may belong to different task. (Good post on activity launch mode)

  1. Why you are checking shared pref in onPreExecute() and then canceling it.? A better way would be to execute asynctask after checking this.

  2. cancel(true) does not guarantee that it will stop async task as soon as called. A better way would be to add isCancelled() check in doInBackground(), if cancelled return result as required. It will help to finish asynctask at earliest without performing task written over there.

  3. In manifest, you might want to add android:conifgChanges for better handing of orientation.

    android:configChanges=["mcc", "mnc", "locale",
    "touchscreen", "keyboard", "keyboardHidden",
    "navigation", "screenLayout", "fontScale",
    "uiMode", "orientation", "screenSize",
    "smallestScreenSize"]

Hope this helps!

Prevent opening Activity for multiple times

Use this:

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

while starting Activity.

from documentation:

If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running.

How to avoid multiple button click at same time in android?

The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second (or any time span).
Example:

// Make your activity class to implement View.OnClickListener
public class MenuPricipalScreen extends Activity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
// setup listeners.
findViewById(R.id.imageView2).setOnClickListener(MenuPricipalScreen.this);
findViewById(R.id.imageView3).setOnClickListener(MenuPricipalScreen.this);
...
}

.
.
.

// variable to track event time
private long mLastClickTime = 0;

// View.OnClickListener.onClick method defination

@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 your stuff.
} else if (v == R.id.imageView3) {
// Do your stuff.
}
...
}

.
.
.

}


Related Topics



Leave a reply



Submit