简体   繁体   中英

Android: Flashing screen when clicked on different ImageButtons

I have 3 ImageButtons that i use for tabs, when they are clicked they load a new activity, but the problem i have is they feel "laggy" and if you click on them multiple times the screen starts flashing and eventually the app crashes, is there any way to get rid of the flashing screen or should i use another method for the tabs?

Here is the code i use:

import android.app.Activity;

public class ContactsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contacts_tab);

    final ImageButton b = (ImageButton) findViewById(R.id.imageButtonCall);
    b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(ContactsActivity.this,CallActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_no_move, R.anim.fade);
        finish();
    }
});

    final ImageButton c = (ImageButton) findViewById(R.id.imageButtonSettings);
    c.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(ContactsActivity.this,SettingsActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_no_move, R.anim.fade);
        finish();
    }
});

What about try to make buttons unable to be clicked more than one time at same time?

Put this in class as global variable:

boolean isBclicked = false;

Now rewrite onClickListener:

final ImageButton b = (ImageButton) findViewById(R.id.imageButtonCall);
    b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
         if( !isBclicked){
           isBclicked = true;
           Intent intent = new Intent(ContactsActivity.this,CallActivity.class);
           startActivity(intent);
           overridePendingTransition(R.anim.slide_no_move, R.anim.fade);
           finish();
         }
   }
});

So basically user won't be able to click multiple times on button, which means that animations won't be interrupted, so your problem will be solved. You can do same thing with other buttons and because after starting new activity you finish current one, there's no need to set "isBclicked" back to false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM