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?
android button android-activity onclick
add a comment |
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?
android button android-activity onclick
You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function
– Maneesh
Nov 10 '11 at 10:02
Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.
– JimmyB
Nov 10 '11 at 10:03
disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...
– Awais Tariq
Nov 10 '11 at 10:05
If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley
– Shylendra Madda
Feb 15 '18 at 6:42
Possible duplicate of Avoid button multiple rapid clicks
– Arnab Kar
Dec 11 '18 at 14:17
add a comment |
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?
android button android-activity onclick
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?
android button android-activity onclick
android button android-activity onclick
edited Nov 28 '18 at 6:18
tejas
asked Nov 10 '11 at 9:59
tejastejas
1,19072851
1,19072851
You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function
– Maneesh
Nov 10 '11 at 10:02
Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.
– JimmyB
Nov 10 '11 at 10:03
disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...
– Awais Tariq
Nov 10 '11 at 10:05
If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley
– Shylendra Madda
Feb 15 '18 at 6:42
Possible duplicate of Avoid button multiple rapid clicks
– Arnab Kar
Dec 11 '18 at 14:17
add a comment |
You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function
– Maneesh
Nov 10 '11 at 10:02
Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.
– JimmyB
Nov 10 '11 at 10:03
disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...
– Awais Tariq
Nov 10 '11 at 10:05
If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley
– Shylendra Madda
Feb 15 '18 at 6:42
Possible duplicate of Avoid button multiple rapid clicks
– Arnab Kar
Dec 11 '18 at 14:17
You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function
– Maneesh
Nov 10 '11 at 10:02
You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function
– Maneesh
Nov 10 '11 at 10:02
Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.
– JimmyB
Nov 10 '11 at 10:03
Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.
– JimmyB
Nov 10 '11 at 10:03
disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...
– Awais Tariq
Nov 10 '11 at 10:05
disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...
– Awais Tariq
Nov 10 '11 at 10:05
If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley
– Shylendra Madda
Feb 15 '18 at 6:42
If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley
– Shylendra Madda
Feb 15 '18 at 6:42
Possible duplicate of Avoid button multiple rapid clicks
– Arnab Kar
Dec 11 '18 at 14:17
Possible duplicate of Avoid button multiple rapid clicks
– Arnab Kar
Dec 11 '18 at 14:17
add a comment |
18 Answers
18
active
oldest
votes
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);
}
This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…
– Martin Marconcini
Mar 5 '18 at 22:06
add a comment |
Add this to your Activity
definition in AndroidManifest.xml
...
android:launchMode = "singleTop"
if helped, then accept the answer..
– Awais Tariq
Nov 10 '11 at 11:48
ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)
– Awais Tariq
Nov 10 '11 at 12:51
2
what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..
– Awais Tariq
Nov 10 '11 at 17:38
13
This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.
– lilbyrdie
May 20 '14 at 15:19
13
This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would beandroid:launchMode = "singleTop"
, which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use thesingleInstance
option.
– Nohus
May 16 '17 at 18:06
|
show 7 more comments
You can use the intent flags like this.
Intent intent = new Intent(Class.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
It will make only one activity be open at the top of the history stack.
2
this should be the correct android-way answer
– rocketspacer
May 16 '16 at 4:26
3
This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest:android:launchMode = "singleTop"
, this way it's solved without having to add the flag to every Intent.
– Nohus
May 16 '17 at 18:09
add a comment |
Since SO doesn't allow me to comment on other answers, I have to pollute this thread with a new answer.
Common answers for the "activity opens twice" problem and my experiences with these solutions (Android 7.1.1):
- Disable button that starts the activity: Works but feels a little clumsy. If you have multiple ways to start the activity in your app (e.g. a button in the action bar AND by clicking on an item in a list view), you have to keep track of the enabled/disabled state of multiple GUI elements. Plus it's not very convenient to disable clicked items in a list view, for example. So, not a very universal approach.
- launchMode="singleInstance": Not working with startActivityForResult(), breaks back navigation with startActivity(), not recommended for regular applications by Android manifest documentation.
- launchMode="singleTask": Not working with startActivityForResult(), not recommended for regular applications by Android manifest documentation.
- FLAG_ACTIVITY_REORDER_TO_FRONT: Breaks back button.
- FLAG_ACTIVITY_SINGLE_TOP: Not working, activity is still opened twice.
- FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me.
EDIT: This was for starting activities with startActivity(). When using startActivityForResult() I need to set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.
FLAG_ACTIVITY_SINGLE_TOP works for me
– Alec von Barnekow
Jun 6 '18 at 16:37
FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1
– Mingjiang Shi
Oct 19 '18 at 4:11
I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?
– Mirmuhsin Sodiqov
Jan 4 at 7:40
I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.
– Mirmuhsin Sodiqov
Jan 4 at 9:08
add a comment |
Let's say @wannik is right but if we have more than 1 button calling same action listener and i click two buttons once almost same time before starting next activity...
So it is good if you have field private boolean mIsClicked = false;
and in the listener:
if(!mIsClicked)
{
mIsClicked = true;
Intent i = new Intent(this, AnotherActitivty.class);
startActivity(i);
}
And onResume()
we need to return the state:
@Override
protected void onResume() {
super.onResume();
mIsClicked = false;
}
What's the deifference between my and @wannik's answer?
If you set enabled to false in the listener of it's calling view other button using same listener will still be enabled. So to be sure that listener's action is not called twice you need to have something global that disables all callings of the listener(nevermind if it's new instance or no)
What is the difference between my answer and others?
They are thinking in right way but they are not thinking for future return to the same instance of the calling activity :)
servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)
– tejas
Jun 24 '13 at 8:02
1
I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity
– Sir NIkolay Cesar The First
Jun 27 '13 at 12:52
1
This isn't sufficient. You need to use asynchronized(mIsClicked) {...}
as well to be 100% safe.
– Monstieur
Dec 20 '13 at 12:59
That's not needed :)
– Sir NIkolay Cesar The First
Dec 21 '13 at 9:33
@Monstieur you don’t need a synchronized block because this is all Main Thread…
– Martin Marconcini
Mar 5 '18 at 21:41
|
show 3 more comments
Use singleInstance to avoid activity to invoke twice.
<activity
android:name=".MainActivity"
android:label="@string/activity"
android:launchMode = "singleInstance" />
add a comment |
I think you're going about solving the problem the wrong way. Generally it is a bad idea for an activity to be making long-running web requests in any of its startup lifecycle methods (onCreate()
, onResume()
, etc). Really these methods should simply be used to instantiate and initialise objects your activity will use and should therefore be relatively quick.
If you need to be performing a web request then do this in a background thread from your newly launched activity (and show the loading dialog in the new activity). Once the background request thread completes it can update the activity and hide the dialog.
This then means your new activity should get launched immediately and prevent the double click from being possible.
add a comment |
Hope this helps:
protected static final int DELAY_TIME = 100;
// to prevent double click issue, disable button after click and enable it after 100ms
protected Handler mClickHandler = new Handler() {
public void handleMessage(Message msg) {
findViewById(msg.what).setClickable(true);
super.handleMessage(msg);
}
};
@Override
public void onClick(View v) {
int id = v.getId();
v.setClickable(false);
mClickHandler.sendEmptyMessageDelayed(id, DELAY_TIME);
// startActivity()
}`
4
lol - what if I click faster then 10x/sec? :D
– Srneczek
Oct 9 '15 at 14:19
add a comment |
It was only working for me when startActivity(intent)
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
add a comment |
For this situation, I'll go for one of two approached, singleTask
in manifest.xml OR a flag in the Activity's onResume()
& onDestroy()
methods respectively.
For the first solution: I prefer to use singleTask
for the activity in the manifest rather than singleInstance
, as per using singleInstance
I figured out that in some occasions the activity creating a new separate instance for itself which result to have a two separate applications window in the running apps in bcakground and besides extra memory allocations that would result a very bad User Experience when the user opens the apps view to choose some app to resume.
So, the better way is to have the activity defined at the manifest.xml like the following:
<activity
android:name=".MainActivity"
android:launchMode="singleTask"</activity>
you can check activity launch modes here.
For the second solution, you have to just define a static variable or a preference variable, for example:
public class MainActivity extends Activity{
public static boolean isRunning = false;
@Override
public void onResume() {
super.onResume();
// now the activity is running
isRunning = true;
}
@Override
public void onDestroy() {
super.onDestroy();
// now the activity will be available again
isRunning = false;
}
}
and from the other side when you want to launch this activity, just check:
private void launchMainActivity(){
if(MainActivity.isRunning)
return;
Intent intent = new Intent(ThisActivity.this, MainActivity.class);
startActivity(intent);
}
add a comment |
Other very very simple solution if you no want use onActivityResult()
is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:
final Button btn = ...
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//start activity here...
btn.setEnabled(false); //disable button
//post a message to run in UI Thread after a delay in milliseconds
btn.postDelayed(new Runnable() {
public void run() {
btn.setEnabled(true); //enable button again
}
},1000); //1 second in this case...
}
});
add a comment |
Just maintain one flag in button onClick method as:
public boolean oneTimeLoadActivity = false;
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!oneTimeLoadActivity){
//start your new activity.
oneTimeLoadActivity = true;
}
}
});
add a comment |
If you're using onActivityResult, you could use a variable to save state.
private Boolean activityOpenInProgress = false;
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if( activityOpenInProgress )
return;
activityOpenInProgress = true;
//Load another activity with startActivityForResult with required request code
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == thatYouSentToOpenActivity ){
activityOpenInProgress = false;
}
}
Works on back button pressed too because request code is returned on event.
add a comment |
Adding:
android:launchMode="singleTop"
Inside the activity tag in AndroidManifest.xml solved the issue.
add a comment |
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myButton.setOnClickListener(null);
}
});
That would probably not work since you would have to declare it as final.
– King
Mar 29 '15 at 11:10
add a comment |
Use a flag
variable set it to true
,
Check if its true just return
else perform Activity Call.
You can also use setClickable(false) one executing the Activity Call
flg=false
public void onClick(View view) {
if(flg==true)
return;
else
{ flg=true;
// perform click}
}
perform click; wait; flg = false;
for when we get back
– Xeno Lupus
Nov 10 '11 at 11:29
add a comment |
You can try this also
Button game = (Button) findViewById(R.id.games);
game.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), Games.class);
startActivityForResult(myIntent, 0);
}
});
This does not work
– tejas
Nov 10 '11 at 12:48
add a comment |
You could just override startActivityForResult and use instance variable:
boolean couldStartActivity = false;
@Override
protected void onResume() {
super.onResume();
couldStartActivity = true;
}
@Override
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
if (couldStartActivity) {
couldStartActivity = false;
intent.putExtra(RequestCodeKey, requestCode);
super.startActivityForResult(intent, requestCode, options);
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8077728%2fhow-to-prevent-the-activity-from-loading-twice-on-pressing-the-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
18 Answers
18
active
oldest
votes
18 Answers
18
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
}
This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…
– Martin Marconcini
Mar 5 '18 at 22:06
add a comment |
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);
}
This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…
– Martin Marconcini
Mar 5 '18 at 22:06
add a comment |
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);
}
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);
}
answered Nov 10 '11 at 10:40
wannikwannik
8,23363453
8,23363453
This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…
– Martin Marconcini
Mar 5 '18 at 22:06
add a comment |
This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…
– Martin Marconcini
Mar 5 '18 at 22:06
This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…
– Martin Marconcini
Mar 5 '18 at 22:06
This is the correct approach. It will even handle Button Selected States for you (if you provide them) and all the Material Design “goodies” you’d expect from a simple standard Widget. I can’t believe people use timers for this. Then you start seeing strange libraries to handle things like these…
– Martin Marconcini
Mar 5 '18 at 22:06
add a comment |
Add this to your Activity
definition in AndroidManifest.xml
...
android:launchMode = "singleTop"
if helped, then accept the answer..
– Awais Tariq
Nov 10 '11 at 11:48
ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)
– Awais Tariq
Nov 10 '11 at 12:51
2
what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..
– Awais Tariq
Nov 10 '11 at 17:38
13
This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.
– lilbyrdie
May 20 '14 at 15:19
13
This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would beandroid:launchMode = "singleTop"
, which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use thesingleInstance
option.
– Nohus
May 16 '17 at 18:06
|
show 7 more comments
Add this to your Activity
definition in AndroidManifest.xml
...
android:launchMode = "singleTop"
if helped, then accept the answer..
– Awais Tariq
Nov 10 '11 at 11:48
ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)
– Awais Tariq
Nov 10 '11 at 12:51
2
what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..
– Awais Tariq
Nov 10 '11 at 17:38
13
This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.
– lilbyrdie
May 20 '14 at 15:19
13
This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would beandroid:launchMode = "singleTop"
, which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use thesingleInstance
option.
– Nohus
May 16 '17 at 18:06
|
show 7 more comments
Add this to your Activity
definition in AndroidManifest.xml
...
android:launchMode = "singleTop"
Add this to your Activity
definition in AndroidManifest.xml
...
android:launchMode = "singleTop"
edited 17 hours ago
Hans Knöchel
5,08271841
5,08271841
answered Nov 10 '11 at 10:04
Awais TariqAwais Tariq
6,12852548
6,12852548
if helped, then accept the answer..
– Awais Tariq
Nov 10 '11 at 11:48
ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)
– Awais Tariq
Nov 10 '11 at 12:51
2
what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..
– Awais Tariq
Nov 10 '11 at 17:38
13
This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.
– lilbyrdie
May 20 '14 at 15:19
13
This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would beandroid:launchMode = "singleTop"
, which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use thesingleInstance
option.
– Nohus
May 16 '17 at 18:06
|
show 7 more comments
if helped, then accept the answer..
– Awais Tariq
Nov 10 '11 at 11:48
ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)
– Awais Tariq
Nov 10 '11 at 12:51
2
what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..
– Awais Tariq
Nov 10 '11 at 17:38
13
This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.
– lilbyrdie
May 20 '14 at 15:19
13
This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would beandroid:launchMode = "singleTop"
, which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use thesingleInstance
option.
– Nohus
May 16 '17 at 18:06
if helped, then accept the answer..
– Awais Tariq
Nov 10 '11 at 11:48
if helped, then accept the answer..
– Awais Tariq
Nov 10 '11 at 11:48
ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)
– Awais Tariq
Nov 10 '11 at 12:51
ok i guess you are doing some long processing after starting new activity.. That's why the screen turns out black. Now if you want to avoid that black screen, you should show a progress dialog at the start of activity and do the long processing in a separate thread (i.e. UI Thread or Simply use async class). Once your processing is done hide that dialog. It is the best solution in my knowledge and I have used it several times...:)
– Awais Tariq
Nov 10 '11 at 12:51
2
2
what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..
– Awais Tariq
Nov 10 '11 at 17:38
what else is possible??? One way or other you must have to implement threading to get a smooth looking app... Try it dude..;) Just put all the current code in a method and call that method from a separate thread at same place where you have written it earlier... It'll hardly increase five to six lines of code..
– Awais Tariq
Nov 10 '11 at 17:38
13
13
This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.
– lilbyrdie
May 20 '14 at 15:19
This prevents two instances of the activity from existing, but it does not prevent the code from running twice, incorrectly. The accepted answer is better, despite having fewer up votes.
– lilbyrdie
May 20 '14 at 15:19
13
13
This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would be
android:launchMode = "singleTop"
, which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use the singleInstance
option.– Nohus
May 16 '17 at 18:06
This is wrong, it makes the activity never exist twice, even in different tasks. The correct way would be
android:launchMode = "singleTop"
, which achieves the effect without breaking Android multitasking. The documentation states that most apps should not use the singleInstance
option.– Nohus
May 16 '17 at 18:06
|
show 7 more comments
You can use the intent flags like this.
Intent intent = new Intent(Class.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
It will make only one activity be open at the top of the history stack.
2
this should be the correct android-way answer
– rocketspacer
May 16 '16 at 4:26
3
This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest:android:launchMode = "singleTop"
, this way it's solved without having to add the flag to every Intent.
– Nohus
May 16 '17 at 18:09
add a comment |
You can use the intent flags like this.
Intent intent = new Intent(Class.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
It will make only one activity be open at the top of the history stack.
2
this should be the correct android-way answer
– rocketspacer
May 16 '16 at 4:26
3
This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest:android:launchMode = "singleTop"
, this way it's solved without having to add the flag to every Intent.
– Nohus
May 16 '17 at 18:09
add a comment |
You can use the intent flags like this.
Intent intent = new Intent(Class.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
It will make only one activity be open at the top of the history stack.
You can use the intent flags like this.
Intent intent = new Intent(Class.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
It will make only one activity be open at the top of the history stack.
edited Oct 3 '17 at 22:45
Darush
4,43033042
4,43033042
answered Feb 20 '14 at 11:47
Carlos EduardoLCarlos EduardoL
487414
487414
2
this should be the correct android-way answer
– rocketspacer
May 16 '16 at 4:26
3
This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest:android:launchMode = "singleTop"
, this way it's solved without having to add the flag to every Intent.
– Nohus
May 16 '17 at 18:09
add a comment |
2
this should be the correct android-way answer
– rocketspacer
May 16 '16 at 4:26
3
This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest:android:launchMode = "singleTop"
, this way it's solved without having to add the flag to every Intent.
– Nohus
May 16 '17 at 18:09
2
2
this should be the correct android-way answer
– rocketspacer
May 16 '16 at 4:26
this should be the correct android-way answer
– rocketspacer
May 16 '16 at 4:26
3
3
This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest:
android:launchMode = "singleTop"
, this way it's solved without having to add the flag to every Intent.– Nohus
May 16 '17 at 18:09
This answer, combined with the most upvoted answer seems to work best. Use this flag in the Activity's manifest:
android:launchMode = "singleTop"
, this way it's solved without having to add the flag to every Intent.– Nohus
May 16 '17 at 18:09
add a comment |
Since SO doesn't allow me to comment on other answers, I have to pollute this thread with a new answer.
Common answers for the "activity opens twice" problem and my experiences with these solutions (Android 7.1.1):
- Disable button that starts the activity: Works but feels a little clumsy. If you have multiple ways to start the activity in your app (e.g. a button in the action bar AND by clicking on an item in a list view), you have to keep track of the enabled/disabled state of multiple GUI elements. Plus it's not very convenient to disable clicked items in a list view, for example. So, not a very universal approach.
- launchMode="singleInstance": Not working with startActivityForResult(), breaks back navigation with startActivity(), not recommended for regular applications by Android manifest documentation.
- launchMode="singleTask": Not working with startActivityForResult(), not recommended for regular applications by Android manifest documentation.
- FLAG_ACTIVITY_REORDER_TO_FRONT: Breaks back button.
- FLAG_ACTIVITY_SINGLE_TOP: Not working, activity is still opened twice.
- FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me.
EDIT: This was for starting activities with startActivity(). When using startActivityForResult() I need to set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.
FLAG_ACTIVITY_SINGLE_TOP works for me
– Alec von Barnekow
Jun 6 '18 at 16:37
FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1
– Mingjiang Shi
Oct 19 '18 at 4:11
I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?
– Mirmuhsin Sodiqov
Jan 4 at 7:40
I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.
– Mirmuhsin Sodiqov
Jan 4 at 9:08
add a comment |
Since SO doesn't allow me to comment on other answers, I have to pollute this thread with a new answer.
Common answers for the "activity opens twice" problem and my experiences with these solutions (Android 7.1.1):
- Disable button that starts the activity: Works but feels a little clumsy. If you have multiple ways to start the activity in your app (e.g. a button in the action bar AND by clicking on an item in a list view), you have to keep track of the enabled/disabled state of multiple GUI elements. Plus it's not very convenient to disable clicked items in a list view, for example. So, not a very universal approach.
- launchMode="singleInstance": Not working with startActivityForResult(), breaks back navigation with startActivity(), not recommended for regular applications by Android manifest documentation.
- launchMode="singleTask": Not working with startActivityForResult(), not recommended for regular applications by Android manifest documentation.
- FLAG_ACTIVITY_REORDER_TO_FRONT: Breaks back button.
- FLAG_ACTIVITY_SINGLE_TOP: Not working, activity is still opened twice.
- FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me.
EDIT: This was for starting activities with startActivity(). When using startActivityForResult() I need to set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.
FLAG_ACTIVITY_SINGLE_TOP works for me
– Alec von Barnekow
Jun 6 '18 at 16:37
FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1
– Mingjiang Shi
Oct 19 '18 at 4:11
I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?
– Mirmuhsin Sodiqov
Jan 4 at 7:40
I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.
– Mirmuhsin Sodiqov
Jan 4 at 9:08
add a comment |
Since SO doesn't allow me to comment on other answers, I have to pollute this thread with a new answer.
Common answers for the "activity opens twice" problem and my experiences with these solutions (Android 7.1.1):
- Disable button that starts the activity: Works but feels a little clumsy. If you have multiple ways to start the activity in your app (e.g. a button in the action bar AND by clicking on an item in a list view), you have to keep track of the enabled/disabled state of multiple GUI elements. Plus it's not very convenient to disable clicked items in a list view, for example. So, not a very universal approach.
- launchMode="singleInstance": Not working with startActivityForResult(), breaks back navigation with startActivity(), not recommended for regular applications by Android manifest documentation.
- launchMode="singleTask": Not working with startActivityForResult(), not recommended for regular applications by Android manifest documentation.
- FLAG_ACTIVITY_REORDER_TO_FRONT: Breaks back button.
- FLAG_ACTIVITY_SINGLE_TOP: Not working, activity is still opened twice.
- FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me.
EDIT: This was for starting activities with startActivity(). When using startActivityForResult() I need to set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.
Since SO doesn't allow me to comment on other answers, I have to pollute this thread with a new answer.
Common answers for the "activity opens twice" problem and my experiences with these solutions (Android 7.1.1):
- Disable button that starts the activity: Works but feels a little clumsy. If you have multiple ways to start the activity in your app (e.g. a button in the action bar AND by clicking on an item in a list view), you have to keep track of the enabled/disabled state of multiple GUI elements. Plus it's not very convenient to disable clicked items in a list view, for example. So, not a very universal approach.
- launchMode="singleInstance": Not working with startActivityForResult(), breaks back navigation with startActivity(), not recommended for regular applications by Android manifest documentation.
- launchMode="singleTask": Not working with startActivityForResult(), not recommended for regular applications by Android manifest documentation.
- FLAG_ACTIVITY_REORDER_TO_FRONT: Breaks back button.
- FLAG_ACTIVITY_SINGLE_TOP: Not working, activity is still opened twice.
- FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me.
EDIT: This was for starting activities with startActivity(). When using startActivityForResult() I need to set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP.
edited Jun 21 '17 at 6:07
answered Jun 8 '17 at 10:37
Andy RoidAndy Roid
12816
12816
FLAG_ACTIVITY_SINGLE_TOP works for me
– Alec von Barnekow
Jun 6 '18 at 16:37
FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1
– Mingjiang Shi
Oct 19 '18 at 4:11
I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?
– Mirmuhsin Sodiqov
Jan 4 at 7:40
I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.
– Mirmuhsin Sodiqov
Jan 4 at 9:08
add a comment |
FLAG_ACTIVITY_SINGLE_TOP works for me
– Alec von Barnekow
Jun 6 '18 at 16:37
FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1
– Mingjiang Shi
Oct 19 '18 at 4:11
I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?
– Mirmuhsin Sodiqov
Jan 4 at 7:40
I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.
– Mirmuhsin Sodiqov
Jan 4 at 9:08
FLAG_ACTIVITY_SINGLE_TOP works for me
– Alec von Barnekow
Jun 6 '18 at 16:37
FLAG_ACTIVITY_SINGLE_TOP works for me
– Alec von Barnekow
Jun 6 '18 at 16:37
FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1
– Mingjiang Shi
Oct 19 '18 at 4:11
FLAG_ACTIVITY_CLEAR_TOP: This is the only one working for me on Android 7.1.1
– Mingjiang Shi
Oct 19 '18 at 4:11
I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?
– Mirmuhsin Sodiqov
Jan 4 at 7:40
I am using "FLAG_ACTIVITY_REORDER_TO_FRONT" and it is working just fine and Back button also acts normal. What exactly did you mean by "Breaks back button"? Could you clarify on that?
– Mirmuhsin Sodiqov
Jan 4 at 7:40
I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.
– Mirmuhsin Sodiqov
Jan 4 at 9:08
I found that "REORDER" flag had a bug... and it was not reordering in KitKat. However, I checked it in Lollipop and Pie, it is working fine.
– Mirmuhsin Sodiqov
Jan 4 at 9:08
add a comment |
Let's say @wannik is right but if we have more than 1 button calling same action listener and i click two buttons once almost same time before starting next activity...
So it is good if you have field private boolean mIsClicked = false;
and in the listener:
if(!mIsClicked)
{
mIsClicked = true;
Intent i = new Intent(this, AnotherActitivty.class);
startActivity(i);
}
And onResume()
we need to return the state:
@Override
protected void onResume() {
super.onResume();
mIsClicked = false;
}
What's the deifference between my and @wannik's answer?
If you set enabled to false in the listener of it's calling view other button using same listener will still be enabled. So to be sure that listener's action is not called twice you need to have something global that disables all callings of the listener(nevermind if it's new instance or no)
What is the difference between my answer and others?
They are thinking in right way but they are not thinking for future return to the same instance of the calling activity :)
servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)
– tejas
Jun 24 '13 at 8:02
1
I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity
– Sir NIkolay Cesar The First
Jun 27 '13 at 12:52
1
This isn't sufficient. You need to use asynchronized(mIsClicked) {...}
as well to be 100% safe.
– Monstieur
Dec 20 '13 at 12:59
That's not needed :)
– Sir NIkolay Cesar The First
Dec 21 '13 at 9:33
@Monstieur you don’t need a synchronized block because this is all Main Thread…
– Martin Marconcini
Mar 5 '18 at 21:41
|
show 3 more comments
Let's say @wannik is right but if we have more than 1 button calling same action listener and i click two buttons once almost same time before starting next activity...
So it is good if you have field private boolean mIsClicked = false;
and in the listener:
if(!mIsClicked)
{
mIsClicked = true;
Intent i = new Intent(this, AnotherActitivty.class);
startActivity(i);
}
And onResume()
we need to return the state:
@Override
protected void onResume() {
super.onResume();
mIsClicked = false;
}
What's the deifference between my and @wannik's answer?
If you set enabled to false in the listener of it's calling view other button using same listener will still be enabled. So to be sure that listener's action is not called twice you need to have something global that disables all callings of the listener(nevermind if it's new instance or no)
What is the difference between my answer and others?
They are thinking in right way but they are not thinking for future return to the same instance of the calling activity :)
servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)
– tejas
Jun 24 '13 at 8:02
1
I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity
– Sir NIkolay Cesar The First
Jun 27 '13 at 12:52
1
This isn't sufficient. You need to use asynchronized(mIsClicked) {...}
as well to be 100% safe.
– Monstieur
Dec 20 '13 at 12:59
That's not needed :)
– Sir NIkolay Cesar The First
Dec 21 '13 at 9:33
@Monstieur you don’t need a synchronized block because this is all Main Thread…
– Martin Marconcini
Mar 5 '18 at 21:41
|
show 3 more comments
Let's say @wannik is right but if we have more than 1 button calling same action listener and i click two buttons once almost same time before starting next activity...
So it is good if you have field private boolean mIsClicked = false;
and in the listener:
if(!mIsClicked)
{
mIsClicked = true;
Intent i = new Intent(this, AnotherActitivty.class);
startActivity(i);
}
And onResume()
we need to return the state:
@Override
protected void onResume() {
super.onResume();
mIsClicked = false;
}
What's the deifference between my and @wannik's answer?
If you set enabled to false in the listener of it's calling view other button using same listener will still be enabled. So to be sure that listener's action is not called twice you need to have something global that disables all callings of the listener(nevermind if it's new instance or no)
What is the difference between my answer and others?
They are thinking in right way but they are not thinking for future return to the same instance of the calling activity :)
Let's say @wannik is right but if we have more than 1 button calling same action listener and i click two buttons once almost same time before starting next activity...
So it is good if you have field private boolean mIsClicked = false;
and in the listener:
if(!mIsClicked)
{
mIsClicked = true;
Intent i = new Intent(this, AnotherActitivty.class);
startActivity(i);
}
And onResume()
we need to return the state:
@Override
protected void onResume() {
super.onResume();
mIsClicked = false;
}
What's the deifference between my and @wannik's answer?
If you set enabled to false in the listener of it's calling view other button using same listener will still be enabled. So to be sure that listener's action is not called twice you need to have something global that disables all callings of the listener(nevermind if it's new instance or no)
What is the difference between my answer and others?
They are thinking in right way but they are not thinking for future return to the same instance of the calling activity :)
edited Jun 24 '13 at 7:49
answered Jun 24 '13 at 7:43
Sir NIkolay Cesar The FirstSir NIkolay Cesar The First
784719
784719
servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)
– tejas
Jun 24 '13 at 8:02
1
I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity
– Sir NIkolay Cesar The First
Jun 27 '13 at 12:52
1
This isn't sufficient. You need to use asynchronized(mIsClicked) {...}
as well to be 100% safe.
– Monstieur
Dec 20 '13 at 12:59
That's not needed :)
– Sir NIkolay Cesar The First
Dec 21 '13 at 9:33
@Monstieur you don’t need a synchronized block because this is all Main Thread…
– Martin Marconcini
Mar 5 '18 at 21:41
|
show 3 more comments
servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)
– tejas
Jun 24 '13 at 8:02
1
I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity
– Sir NIkolay Cesar The First
Jun 27 '13 at 12:52
1
This isn't sufficient. You need to use asynchronized(mIsClicked) {...}
as well to be 100% safe.
– Monstieur
Dec 20 '13 at 12:59
That's not needed :)
– Sir NIkolay Cesar The First
Dec 21 '13 at 9:33
@Monstieur you don’t need a synchronized block because this is all Main Thread…
– Martin Marconcini
Mar 5 '18 at 21:41
servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)
– tejas
Jun 24 '13 at 8:02
servoper, thank you for your research. This question has been already been solved, how ever your answer also looks promising for the situation you told. Let me try and come with the result :)
– tejas
Jun 24 '13 at 8:02
1
1
I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity
– Sir NIkolay Cesar The First
Jun 27 '13 at 12:52
I have this issue in one of my games. I have "select level" baloons that have same listener and the views are just different by tags. So if I fast choose two baloons it starts two activities. I know that because the new activity starts sound.. and in this case sound is played twice... but you can check it by clicking back which will drive you to previous activity
– Sir NIkolay Cesar The First
Jun 27 '13 at 12:52
1
1
This isn't sufficient. You need to use a
synchronized(mIsClicked) {...}
as well to be 100% safe.– Monstieur
Dec 20 '13 at 12:59
This isn't sufficient. You need to use a
synchronized(mIsClicked) {...}
as well to be 100% safe.– Monstieur
Dec 20 '13 at 12:59
That's not needed :)
– Sir NIkolay Cesar The First
Dec 21 '13 at 9:33
That's not needed :)
– Sir NIkolay Cesar The First
Dec 21 '13 at 9:33
@Monstieur you don’t need a synchronized block because this is all Main Thread…
– Martin Marconcini
Mar 5 '18 at 21:41
@Monstieur you don’t need a synchronized block because this is all Main Thread…
– Martin Marconcini
Mar 5 '18 at 21:41
|
show 3 more comments
Use singleInstance to avoid activity to invoke twice.
<activity
android:name=".MainActivity"
android:label="@string/activity"
android:launchMode = "singleInstance" />
add a comment |
Use singleInstance to avoid activity to invoke twice.
<activity
android:name=".MainActivity"
android:label="@string/activity"
android:launchMode = "singleInstance" />
add a comment |
Use singleInstance to avoid activity to invoke twice.
<activity
android:name=".MainActivity"
android:label="@string/activity"
android:launchMode = "singleInstance" />
Use singleInstance to avoid activity to invoke twice.
<activity
android:name=".MainActivity"
android:label="@string/activity"
android:launchMode = "singleInstance" />
answered Jun 3 '16 at 13:44
Manvendra PriyadarshiManvendra Priyadarshi
15715
15715
add a comment |
add a comment |
I think you're going about solving the problem the wrong way. Generally it is a bad idea for an activity to be making long-running web requests in any of its startup lifecycle methods (onCreate()
, onResume()
, etc). Really these methods should simply be used to instantiate and initialise objects your activity will use and should therefore be relatively quick.
If you need to be performing a web request then do this in a background thread from your newly launched activity (and show the loading dialog in the new activity). Once the background request thread completes it can update the activity and hide the dialog.
This then means your new activity should get launched immediately and prevent the double click from being possible.
add a comment |
I think you're going about solving the problem the wrong way. Generally it is a bad idea for an activity to be making long-running web requests in any of its startup lifecycle methods (onCreate()
, onResume()
, etc). Really these methods should simply be used to instantiate and initialise objects your activity will use and should therefore be relatively quick.
If you need to be performing a web request then do this in a background thread from your newly launched activity (and show the loading dialog in the new activity). Once the background request thread completes it can update the activity and hide the dialog.
This then means your new activity should get launched immediately and prevent the double click from being possible.
add a comment |
I think you're going about solving the problem the wrong way. Generally it is a bad idea for an activity to be making long-running web requests in any of its startup lifecycle methods (onCreate()
, onResume()
, etc). Really these methods should simply be used to instantiate and initialise objects your activity will use and should therefore be relatively quick.
If you need to be performing a web request then do this in a background thread from your newly launched activity (and show the loading dialog in the new activity). Once the background request thread completes it can update the activity and hide the dialog.
This then means your new activity should get launched immediately and prevent the double click from being possible.
I think you're going about solving the problem the wrong way. Generally it is a bad idea for an activity to be making long-running web requests in any of its startup lifecycle methods (onCreate()
, onResume()
, etc). Really these methods should simply be used to instantiate and initialise objects your activity will use and should therefore be relatively quick.
If you need to be performing a web request then do this in a background thread from your newly launched activity (and show the loading dialog in the new activity). Once the background request thread completes it can update the activity and hide the dialog.
This then means your new activity should get launched immediately and prevent the double click from being possible.
answered Nov 10 '11 at 10:07
tomtheguvnortomtheguvnor
2,63812033
2,63812033
add a comment |
add a comment |
Hope this helps:
protected static final int DELAY_TIME = 100;
// to prevent double click issue, disable button after click and enable it after 100ms
protected Handler mClickHandler = new Handler() {
public void handleMessage(Message msg) {
findViewById(msg.what).setClickable(true);
super.handleMessage(msg);
}
};
@Override
public void onClick(View v) {
int id = v.getId();
v.setClickable(false);
mClickHandler.sendEmptyMessageDelayed(id, DELAY_TIME);
// startActivity()
}`
4
lol - what if I click faster then 10x/sec? :D
– Srneczek
Oct 9 '15 at 14:19
add a comment |
Hope this helps:
protected static final int DELAY_TIME = 100;
// to prevent double click issue, disable button after click and enable it after 100ms
protected Handler mClickHandler = new Handler() {
public void handleMessage(Message msg) {
findViewById(msg.what).setClickable(true);
super.handleMessage(msg);
}
};
@Override
public void onClick(View v) {
int id = v.getId();
v.setClickable(false);
mClickHandler.sendEmptyMessageDelayed(id, DELAY_TIME);
// startActivity()
}`
4
lol - what if I click faster then 10x/sec? :D
– Srneczek
Oct 9 '15 at 14:19
add a comment |
Hope this helps:
protected static final int DELAY_TIME = 100;
// to prevent double click issue, disable button after click and enable it after 100ms
protected Handler mClickHandler = new Handler() {
public void handleMessage(Message msg) {
findViewById(msg.what).setClickable(true);
super.handleMessage(msg);
}
};
@Override
public void onClick(View v) {
int id = v.getId();
v.setClickable(false);
mClickHandler.sendEmptyMessageDelayed(id, DELAY_TIME);
// startActivity()
}`
Hope this helps:
protected static final int DELAY_TIME = 100;
// to prevent double click issue, disable button after click and enable it after 100ms
protected Handler mClickHandler = new Handler() {
public void handleMessage(Message msg) {
findViewById(msg.what).setClickable(true);
super.handleMessage(msg);
}
};
@Override
public void onClick(View v) {
int id = v.getId();
v.setClickable(false);
mClickHandler.sendEmptyMessageDelayed(id, DELAY_TIME);
// startActivity()
}`
answered May 23 '12 at 7:38
thanhbinh84thanhbinh84
11.8k33746
11.8k33746
4
lol - what if I click faster then 10x/sec? :D
– Srneczek
Oct 9 '15 at 14:19
add a comment |
4
lol - what if I click faster then 10x/sec? :D
– Srneczek
Oct 9 '15 at 14:19
4
4
lol - what if I click faster then 10x/sec? :D
– Srneczek
Oct 9 '15 at 14:19
lol - what if I click faster then 10x/sec? :D
– Srneczek
Oct 9 '15 at 14:19
add a comment |
It was only working for me when startActivity(intent)
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
add a comment |
It was only working for me when startActivity(intent)
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
add a comment |
It was only working for me when startActivity(intent)
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
It was only working for me when startActivity(intent)
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
answered Oct 25 '17 at 6:49
Shylendra MaddaShylendra Madda
11.4k64783
11.4k64783
add a comment |
add a comment |
For this situation, I'll go for one of two approached, singleTask
in manifest.xml OR a flag in the Activity's onResume()
& onDestroy()
methods respectively.
For the first solution: I prefer to use singleTask
for the activity in the manifest rather than singleInstance
, as per using singleInstance
I figured out that in some occasions the activity creating a new separate instance for itself which result to have a two separate applications window in the running apps in bcakground and besides extra memory allocations that would result a very bad User Experience when the user opens the apps view to choose some app to resume.
So, the better way is to have the activity defined at the manifest.xml like the following:
<activity
android:name=".MainActivity"
android:launchMode="singleTask"</activity>
you can check activity launch modes here.
For the second solution, you have to just define a static variable or a preference variable, for example:
public class MainActivity extends Activity{
public static boolean isRunning = false;
@Override
public void onResume() {
super.onResume();
// now the activity is running
isRunning = true;
}
@Override
public void onDestroy() {
super.onDestroy();
// now the activity will be available again
isRunning = false;
}
}
and from the other side when you want to launch this activity, just check:
private void launchMainActivity(){
if(MainActivity.isRunning)
return;
Intent intent = new Intent(ThisActivity.this, MainActivity.class);
startActivity(intent);
}
add a comment |
For this situation, I'll go for one of two approached, singleTask
in manifest.xml OR a flag in the Activity's onResume()
& onDestroy()
methods respectively.
For the first solution: I prefer to use singleTask
for the activity in the manifest rather than singleInstance
, as per using singleInstance
I figured out that in some occasions the activity creating a new separate instance for itself which result to have a two separate applications window in the running apps in bcakground and besides extra memory allocations that would result a very bad User Experience when the user opens the apps view to choose some app to resume.
So, the better way is to have the activity defined at the manifest.xml like the following:
<activity
android:name=".MainActivity"
android:launchMode="singleTask"</activity>
you can check activity launch modes here.
For the second solution, you have to just define a static variable or a preference variable, for example:
public class MainActivity extends Activity{
public static boolean isRunning = false;
@Override
public void onResume() {
super.onResume();
// now the activity is running
isRunning = true;
}
@Override
public void onDestroy() {
super.onDestroy();
// now the activity will be available again
isRunning = false;
}
}
and from the other side when you want to launch this activity, just check:
private void launchMainActivity(){
if(MainActivity.isRunning)
return;
Intent intent = new Intent(ThisActivity.this, MainActivity.class);
startActivity(intent);
}
add a comment |
For this situation, I'll go for one of two approached, singleTask
in manifest.xml OR a flag in the Activity's onResume()
& onDestroy()
methods respectively.
For the first solution: I prefer to use singleTask
for the activity in the manifest rather than singleInstance
, as per using singleInstance
I figured out that in some occasions the activity creating a new separate instance for itself which result to have a two separate applications window in the running apps in bcakground and besides extra memory allocations that would result a very bad User Experience when the user opens the apps view to choose some app to resume.
So, the better way is to have the activity defined at the manifest.xml like the following:
<activity
android:name=".MainActivity"
android:launchMode="singleTask"</activity>
you can check activity launch modes here.
For the second solution, you have to just define a static variable or a preference variable, for example:
public class MainActivity extends Activity{
public static boolean isRunning = false;
@Override
public void onResume() {
super.onResume();
// now the activity is running
isRunning = true;
}
@Override
public void onDestroy() {
super.onDestroy();
// now the activity will be available again
isRunning = false;
}
}
and from the other side when you want to launch this activity, just check:
private void launchMainActivity(){
if(MainActivity.isRunning)
return;
Intent intent = new Intent(ThisActivity.this, MainActivity.class);
startActivity(intent);
}
For this situation, I'll go for one of two approached, singleTask
in manifest.xml OR a flag in the Activity's onResume()
& onDestroy()
methods respectively.
For the first solution: I prefer to use singleTask
for the activity in the manifest rather than singleInstance
, as per using singleInstance
I figured out that in some occasions the activity creating a new separate instance for itself which result to have a two separate applications window in the running apps in bcakground and besides extra memory allocations that would result a very bad User Experience when the user opens the apps view to choose some app to resume.
So, the better way is to have the activity defined at the manifest.xml like the following:
<activity
android:name=".MainActivity"
android:launchMode="singleTask"</activity>
you can check activity launch modes here.
For the second solution, you have to just define a static variable or a preference variable, for example:
public class MainActivity extends Activity{
public static boolean isRunning = false;
@Override
public void onResume() {
super.onResume();
// now the activity is running
isRunning = true;
}
@Override
public void onDestroy() {
super.onDestroy();
// now the activity will be available again
isRunning = false;
}
}
and from the other side when you want to launch this activity, just check:
private void launchMainActivity(){
if(MainActivity.isRunning)
return;
Intent intent = new Intent(ThisActivity.this, MainActivity.class);
startActivity(intent);
}
answered Apr 9 '17 at 11:38
Muhammed RefaatMuhammed Refaat
5,96296092
5,96296092
add a comment |
add a comment |
Other very very simple solution if you no want use onActivityResult()
is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:
final Button btn = ...
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//start activity here...
btn.setEnabled(false); //disable button
//post a message to run in UI Thread after a delay in milliseconds
btn.postDelayed(new Runnable() {
public void run() {
btn.setEnabled(true); //enable button again
}
},1000); //1 second in this case...
}
});
add a comment |
Other very very simple solution if you no want use onActivityResult()
is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:
final Button btn = ...
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//start activity here...
btn.setEnabled(false); //disable button
//post a message to run in UI Thread after a delay in milliseconds
btn.postDelayed(new Runnable() {
public void run() {
btn.setEnabled(true); //enable button again
}
},1000); //1 second in this case...
}
});
add a comment |
Other very very simple solution if you no want use onActivityResult()
is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:
final Button btn = ...
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//start activity here...
btn.setEnabled(false); //disable button
//post a message to run in UI Thread after a delay in milliseconds
btn.postDelayed(new Runnable() {
public void run() {
btn.setEnabled(true); //enable button again
}
},1000); //1 second in this case...
}
});
Other very very simple solution if you no want use onActivityResult()
is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:
final Button btn = ...
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//start activity here...
btn.setEnabled(false); //disable button
//post a message to run in UI Thread after a delay in milliseconds
btn.postDelayed(new Runnable() {
public void run() {
btn.setEnabled(true); //enable button again
}
},1000); //1 second in this case...
}
});
answered Jun 18 '16 at 18:54
GilianGilian
227511
227511
add a comment |
add a comment |
Just maintain one flag in button onClick method as:
public boolean oneTimeLoadActivity = false;
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!oneTimeLoadActivity){
//start your new activity.
oneTimeLoadActivity = true;
}
}
});
add a comment |
Just maintain one flag in button onClick method as:
public boolean oneTimeLoadActivity = false;
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!oneTimeLoadActivity){
//start your new activity.
oneTimeLoadActivity = true;
}
}
});
add a comment |
Just maintain one flag in button onClick method as:
public boolean oneTimeLoadActivity = false;
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!oneTimeLoadActivity){
//start your new activity.
oneTimeLoadActivity = true;
}
}
});
Just maintain one flag in button onClick method as:
public boolean oneTimeLoadActivity = false;
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!oneTimeLoadActivity){
//start your new activity.
oneTimeLoadActivity = true;
}
}
});
answered Nov 10 '11 at 10:05
Balaji KhadakeBalaji Khadake
2,78342035
2,78342035
add a comment |
add a comment |
If you're using onActivityResult, you could use a variable to save state.
private Boolean activityOpenInProgress = false;
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if( activityOpenInProgress )
return;
activityOpenInProgress = true;
//Load another activity with startActivityForResult with required request code
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == thatYouSentToOpenActivity ){
activityOpenInProgress = false;
}
}
Works on back button pressed too because request code is returned on event.
add a comment |
If you're using onActivityResult, you could use a variable to save state.
private Boolean activityOpenInProgress = false;
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if( activityOpenInProgress )
return;
activityOpenInProgress = true;
//Load another activity with startActivityForResult with required request code
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == thatYouSentToOpenActivity ){
activityOpenInProgress = false;
}
}
Works on back button pressed too because request code is returned on event.
add a comment |
If you're using onActivityResult, you could use a variable to save state.
private Boolean activityOpenInProgress = false;
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if( activityOpenInProgress )
return;
activityOpenInProgress = true;
//Load another activity with startActivityForResult with required request code
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == thatYouSentToOpenActivity ){
activityOpenInProgress = false;
}
}
Works on back button pressed too because request code is returned on event.
If you're using onActivityResult, you could use a variable to save state.
private Boolean activityOpenInProgress = false;
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if( activityOpenInProgress )
return;
activityOpenInProgress = true;
//Load another activity with startActivityForResult with required request code
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == thatYouSentToOpenActivity ){
activityOpenInProgress = false;
}
}
Works on back button pressed too because request code is returned on event.
answered Oct 30 '15 at 22:01
UmangUmang
196111
196111
add a comment |
add a comment |
Adding:
android:launchMode="singleTop"
Inside the activity tag in AndroidManifest.xml solved the issue.
add a comment |
Adding:
android:launchMode="singleTop"
Inside the activity tag in AndroidManifest.xml solved the issue.
add a comment |
Adding:
android:launchMode="singleTop"
Inside the activity tag in AndroidManifest.xml solved the issue.
Adding:
android:launchMode="singleTop"
Inside the activity tag in AndroidManifest.xml solved the issue.
answered Oct 8 '18 at 12:42
Fabio TrottaFabio Trotta
4616
4616
add a comment |
add a comment |
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myButton.setOnClickListener(null);
}
});
That would probably not work since you would have to declare it as final.
– King
Mar 29 '15 at 11:10
add a comment |
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myButton.setOnClickListener(null);
}
});
That would probably not work since you would have to declare it as final.
– King
Mar 29 '15 at 11:10
add a comment |
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myButton.setOnClickListener(null);
}
});
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myButton.setOnClickListener(null);
}
});
answered Nov 10 '11 at 10:04
Thunder RabbitThunder Rabbit
3,32953777
3,32953777
That would probably not work since you would have to declare it as final.
– King
Mar 29 '15 at 11:10
add a comment |
That would probably not work since you would have to declare it as final.
– King
Mar 29 '15 at 11:10
That would probably not work since you would have to declare it as final.
– King
Mar 29 '15 at 11:10
That would probably not work since you would have to declare it as final.
– King
Mar 29 '15 at 11:10
add a comment |
Use a flag
variable set it to true
,
Check if its true just return
else perform Activity Call.
You can also use setClickable(false) one executing the Activity Call
flg=false
public void onClick(View view) {
if(flg==true)
return;
else
{ flg=true;
// perform click}
}
perform click; wait; flg = false;
for when we get back
– Xeno Lupus
Nov 10 '11 at 11:29
add a comment |
Use a flag
variable set it to true
,
Check if its true just return
else perform Activity Call.
You can also use setClickable(false) one executing the Activity Call
flg=false
public void onClick(View view) {
if(flg==true)
return;
else
{ flg=true;
// perform click}
}
perform click; wait; flg = false;
for when we get back
– Xeno Lupus
Nov 10 '11 at 11:29
add a comment |
Use a flag
variable set it to true
,
Check if its true just return
else perform Activity Call.
You can also use setClickable(false) one executing the Activity Call
flg=false
public void onClick(View view) {
if(flg==true)
return;
else
{ flg=true;
// perform click}
}
Use a flag
variable set it to true
,
Check if its true just return
else perform Activity Call.
You can also use setClickable(false) one executing the Activity Call
flg=false
public void onClick(View view) {
if(flg==true)
return;
else
{ flg=true;
// perform click}
}
answered Nov 10 '11 at 10:04
MKJParekhMKJParekh
29.8k107995
29.8k107995
perform click; wait; flg = false;
for when we get back
– Xeno Lupus
Nov 10 '11 at 11:29
add a comment |
perform click; wait; flg = false;
for when we get back
– Xeno Lupus
Nov 10 '11 at 11:29
perform click; wait; flg = false;
for when we get back– Xeno Lupus
Nov 10 '11 at 11:29
perform click; wait; flg = false;
for when we get back– Xeno Lupus
Nov 10 '11 at 11:29
add a comment |
You can try this also
Button game = (Button) findViewById(R.id.games);
game.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), Games.class);
startActivityForResult(myIntent, 0);
}
});
This does not work
– tejas
Nov 10 '11 at 12:48
add a comment |
You can try this also
Button game = (Button) findViewById(R.id.games);
game.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), Games.class);
startActivityForResult(myIntent, 0);
}
});
This does not work
– tejas
Nov 10 '11 at 12:48
add a comment |
You can try this also
Button game = (Button) findViewById(R.id.games);
game.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), Games.class);
startActivityForResult(myIntent, 0);
}
});
You can try this also
Button game = (Button) findViewById(R.id.games);
game.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), Games.class);
startActivityForResult(myIntent, 0);
}
});
answered Nov 10 '11 at 10:48
KarthikKarthik
2,504184480
2,504184480
This does not work
– tejas
Nov 10 '11 at 12:48
add a comment |
This does not work
– tejas
Nov 10 '11 at 12:48
This does not work
– tejas
Nov 10 '11 at 12:48
This does not work
– tejas
Nov 10 '11 at 12:48
add a comment |
You could just override startActivityForResult and use instance variable:
boolean couldStartActivity = false;
@Override
protected void onResume() {
super.onResume();
couldStartActivity = true;
}
@Override
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
if (couldStartActivity) {
couldStartActivity = false;
intent.putExtra(RequestCodeKey, requestCode);
super.startActivityForResult(intent, requestCode, options);
}
}
add a comment |
You could just override startActivityForResult and use instance variable:
boolean couldStartActivity = false;
@Override
protected void onResume() {
super.onResume();
couldStartActivity = true;
}
@Override
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
if (couldStartActivity) {
couldStartActivity = false;
intent.putExtra(RequestCodeKey, requestCode);
super.startActivityForResult(intent, requestCode, options);
}
}
add a comment |
You could just override startActivityForResult and use instance variable:
boolean couldStartActivity = false;
@Override
protected void onResume() {
super.onResume();
couldStartActivity = true;
}
@Override
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
if (couldStartActivity) {
couldStartActivity = false;
intent.putExtra(RequestCodeKey, requestCode);
super.startActivityForResult(intent, requestCode, options);
}
}
You could just override startActivityForResult and use instance variable:
boolean couldStartActivity = false;
@Override
protected void onResume() {
super.onResume();
couldStartActivity = true;
}
@Override
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
if (couldStartActivity) {
couldStartActivity = false;
intent.putExtra(RequestCodeKey, requestCode);
super.startActivityForResult(intent, requestCode, options);
}
}
answered Jul 17 '15 at 17:42
Aleksei MinaevAleksei Minaev
8461015
8461015
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8077728%2fhow-to-prevent-the-activity-from-loading-twice-on-pressing-the-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can disable the button after opening the activity...and when activity finish,,re-enable it...u can detect the finish of second activity by calling onActivityResult function
– Maneesh
Nov 10 '11 at 10:02
Disable the button when it is first clicked and re-enable it later only when you want the button to be clicked again.
– JimmyB
Nov 10 '11 at 10:03
disabling doesn't work in a simple manner if the very next statement is for some long process or activity start... To disable button you must have to create a separate thread...
– Awais Tariq
Nov 10 '11 at 10:05
If hitting the same API twice refer here: techstricks.com/avoid-multiple-requests-when-using-volley
– Shylendra Madda
Feb 15 '18 at 6:42
Possible duplicate of Avoid button multiple rapid clicks
– Arnab Kar
Dec 11 '18 at 14:17