Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 700
tejas Avatar asked Nov 10 '11 09:11

tejas


People also ask

How do I turn off multiple click on Android?

Show activity on this post. call setClickable(false) for all buttons once one of them was clicked. call the next activity with startActivityForResult(...) override onActivityResult(...) and call setClickable(true) for all buttons inside it.


2 Answers

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"/> 
like image 186
Awais Tariq Avatar answered Sep 26 '22 00:09

Awais Tariq


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);     } 
like image 20
wannik Avatar answered Sep 24 '22 00:09

wannik