Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...