Goofy
Goofy

Reputation: 6128

Maintain Single instance of an activity

i have button in my first activity called Start.

Now when i click on this button it takes 1 to 2 seconds to load the next activity, now at that time the user clicks on the start button multiple times so what happens is that the next activity will open multiple times.

How to overcome this? Is there any way that even if the user clicks on the Start button multiple times open the activity only once.

Upvotes: 0

Views: 485

Answers (3)

Archie.bpgc
Archie.bpgc

Reputation: 24012

Don't use anything like a launchMode or Intent flags. They are used for different purposes.

Description here

What you have to do is:

  1. Show a progress dialog to clearly show the user that an action(calling 2nd Activity) is in progress. This was user will not try to click the button multiple times
  2. Simply disable the button's click listener after 1st click. This is not recommended because user might not be able to know whether he/she clicked the button. Also this is the case where user tends to click the button multiple times.

Upvotes: 1

Tushar
Tushar

Reputation: 8049

Your Options:

  • On click, disable the button and display a ProgressDialog to the user.
  • Use the Intent flag FLAG_ACTIVITY_SINGLE_TOP to ensure only one activity is maintained on the stack. Documentation
  • Use the qualifer launchMode=singleInstance in your AndroidManifest.xml so only one instance of your Activity is allowed at a time. Documentation

I would recommend the first, because it can show the user your application is still working even if it takes a few seconds to do the necessary processing to begin your Activity.

Upvotes: 1

Kameswari
Kameswari

Reputation: 758

You can put the launch mode of your 2nd activity as "Single Instance" in your manifest file.

Upvotes: 1

Related Questions