How to prevent an activity from being initialized multiple times - android

I have an Android app, when a user taps a button multiple times quickly, same activity is initialized multiple times.
To prevent this, I added android:launchMode="singleInstance" in Manifest file. But now, when an activity calls itself, it doesn't work.
I also tried
Intent myintent = getIntent();
myintent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
But this didn't work either.
How can I prevent having multiple activities when user clicks a button multiple times quickly, and how can I have same activity call itself correctly. Thanks.

Yeah, this happens if you are "trigger happy". You can also in many situations use multi-touch to activate a bunch of options at the same time. If you really need to solve this, you can look at disabling elements like J Whitfield suggested (element.setEnabled(false) or element.setClickable(false)) or intercepting onTouch.

You could try disabling the button after the first click has been detected.
Button button = theView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.isEnabled()){
v.setEnabled(false);
}
//Call your new activity here
...activity stuff...
}
});

Related

Only make it clickable if....?

I have an application, where you can click on the background, and it's changes from the drawables to another background, but I only want to make it clickable when the user click on a button that i call, "I want to click it".
So, how to write a code like:
If user click on button1 2 times, make layout clickable
else
not make layout clickable
So, I want to store somehow the click, and force my app to remember to it, and I also want to count the clicks.
What chapter of Android are helping me understanding this? Thanks for help, and sorry for the noobish question:)
You can have counter that increments on click and then just disable component when counter reach value that you want
int count = 0;
Button button = (Button) findViewById(R.id.i_want_to_click_it);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (++count == 2) {
// make stuff clickable here on 2nd click
}
// if you also want to make things unclickable if there
// are more than 2 clicks, add the else{} condition
else {
// make stuff unclickable here
}
}
});
You can detect when user clicks a button by registering a onClickListener on it. Inside that callback you can count how many times it has been clicked and store that information inside a variable. If your app can change from portrait to landscape mode, dont forget to store the variable in onSavedInstanceState and then retrieve it in onCreate, because changing layout mode will destroy the activity and rebuild it which will reset your variable. I highlighted keywords to search for.

Android Device Backbutton should not work

First of all i am learning android, I am testing my Andorid app in my Samsung Galaxy S1.
My app Function is: while i am pressing RandomNumber button, it will generate Random numbers and displaying in the screen in TextArea.
But i am facing the below issues.
The Device back button is allow user to go back. How i can avoid that? ( I have buttons defined in the program dynamically, only that Back button should work )
While shaking the phone or change the position of the phone, then the Random numbers are automatically generating. How to avoid that?. Please advise.
Button Creation Dynamic
final Button buttonToAdd = new Button(this);
buttonToAdd.setText("RandomNumber");
Listener:
buttonToAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Strvalue = (String) buttonToAdd.getText();
if (Strvalue.equals("RandomNumber"))
{
Randomnumbergeneration();
}
}
});
You can overwrite the Activities onBackPressed() method to handle the back-button click event.
#Override
public void onBackPressed() {
// put some code here or just do nothing
// don't call super.onBackPressed() if you want to disable the back function
}
But if you want to publish your application you should follow the official design guidelines and do not disable this behaviour because every android user is used to it and will find it unlikely if the back button does not work anymore.

Do Button events in Android really have to be explicitly disabled?

So I'm new to Android and have this wee app that has a variety of Buttons. The buttons do a variety of things, but of particular interest are the buttons that intent to another activity.
Because as I'm happily programming and testing along, I discover that I can double- and sometimes triple-tap these buttons.
I look for methods on the Button object that will allow me to specify the number of clicks that the button is allowed or whether the button should be (even briefly) disabled after a click. I find nothing of the sort.
Incredulous, I begin googling for a high-level discussion of this strange behavior. I find no interesting discussions, just suggestions about how to handle the issue on every single button in my app.
With a heavy sigh, I surrender to the time demands of my project, and add private variables to my activities (no static locals in Java. crap.), which the click-handling method uses to tell whether it's already busy handling a button click.
But still I wonder. Do Button events in Android really have to be explicitly disabled?
Edit: I'm looking for an answer of the form: "Yes (or no), and I know they have to be explicitly disabled because X".
The platform can't assume you only want to allow the button to be clicked once, or how frequently you should be able to click it. Just add logic to disable the button once you've clicked it, e.g.:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
v.setEnabled(false);
//Do other stuf
}
});
You can use droidQuery to set a button or a set of buttons to handle the first click event, then not any future click events. For example:
$.with(button).one("click", new Function() {
#Override
public void invoke($ d, Object... params) {
//Do stuff - this will only happen on the first click event
}
});

Having a refresh button

Lets suppose I am on screen2 on my application, and my application retrieves data from a database. I want to have a refresh button on it. So I am writing this code:
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Screen2.this,Screen2.class);
startActivity(i);
}
});
where b3 is the button.My question is kind of theoritically: Is there any better way? Does this make my application heavy or causes any other problems to OS if I do for example several time refresh.
Would it be better to do the refresh for example at specific times? If yes, how I will write this code?
Thanks.
you could use adapter.notifyDataSetChanged(). much more efficient
http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged()
Edit: (thats only valid if you are using an ArrayAdapter)
You should not restart the activity each time you want updated information: just update your views when the update button is clicked.

Is it possible to chain event listeners in Android?

I have an activity which contains QuickContactBadges. I'm looking for a way to either chain event listeners on the QuickContactBadge, or to call the default listener from within an override.
Specifically, what I am looking to do is have the QuickContactBadge, when clicked to show the QuickContact card, and then to setResult and finish, to close my activity.
So either I want to add a second listener to the badge in addition to the default one, or implement something like the following:
bdg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
QuickContactBadge bdg = (QuickContactBadge) view;
bdg.base.onClick(); // PSEUDO-CODE LINE
setResult(RESULT_CANCELED, null);
finish();
}
});
Are either of these methods possible, or is there some other way I should be doing this?
Well, the answer to what I was trying to do was not actually in an event listener at all.
The key to getting my activity to close when the QuickBadge is clicked was to add android:noHistory="true" to the activity definition in the application manifest file.
Though, it would still be interesting to know yes/no if there is a way to chain event listeners.

Categories

Resources