I wonder that if I have two buttons, and every time I press the first button I call the method secondButton.setOnClickListener(new OnClickListener). My question is when I set the new listener for the second button every time I press the first button, what happens to the old listener is it still in the memory ?
Activity owns button, button has a listener that is owned by Activity, if both can't be accessed from root, then theoretically they should be GCed.
there is a better way.
secondButton.performClick();
https://developer.android.com/reference/android/view/View.html#performClick()
Hope it helps.
Related
I have an activity, DogActivity, with a slider. When the user slides view PawsView to a certain degree, I start another activity, CatActivity, using startActivity(intent). If the user clicks the back button, normally the user returns to DogActivity. Here is my problem: if in Developer options I set Do not keep activities then when the user clicks the back button and thus returns to DogActivity, the slider is not asserted and so PawsView is back to its original position; however, if I don't have that option selected, upon returning to DogActivity the slider is still asserted (the sliding already occurred).
Since I don't want to depend on the user selecting or deselecting Do not keep activities, I need to do this programmatically. So does anyone know how to do this? I have tried putting the appropriate code inside onResume but that has no effect. It's as if finishing CatActivity has no effect on DogActivity. BTW, the view I am using to display PawsView is a custom view.
I already tried using a handler with postDelayed to pull PawsView back to normal, but the handler always executes before the startActivity is executed. If on the other hand I start a proper Thread to run the call to close the slider, I get an error about the wrong thread trying to change the layout.
Another way of asking the question may be: How do I force onResume to be called even when Do not keep activities is NOT selected on a user's device.
You could try to launch CatActivity using startActivityForResult and then handle the result in onActivityResult and do the necessary setup from there. It's sort of like forcing onResume.
i have button in my first activity called Start.
Now when i click on this button it takes 1 to 2 seconds to load the next activity, now at that time the user clicks on the start button multiple times so what happens is that the next activity will open multiple times.
How to overcome this? Is there any way that even if the user clicks on the Start button multiple times open the activity only once.
Your Options:
On click, disable the button and display a ProgressDialog to the user.
Use the Intent flag FLAG_ACTIVITY_SINGLE_TOP to ensure only one activity is maintained on the stack. Documentation
Use the qualifer launchMode=singleInstance in your AndroidManifest.xml so only one instance of your Activity is allowed at a time. Documentation
I would recommend the first, because it can show the user your application is still working even if it takes a few seconds to do the necessary processing to begin your Activity.
You can put the launch mode of your 2nd activity as "Single Instance" in your manifest file.
Don't use anything like a launchMode or Intent flags. They are used for different purposes.
Description here
What you have to do is:
Show a progress dialog to clearly show the user that an
action(calling 2nd Activity) is in progress. This was user will not
try to click the button multiple times
Simply disable the button's click listener after 1st click. This is not
recommended because user might not be able to know whether he/she
clicked the button. Also this is the case where user tends to click
the button multiple times.
Good Morning,
I have a list of items with a prev. & next button. When the user is at the start of the list the prev button is disabled. Clicking next takes them to the next record and my click handler sets the prev button enabled true. However in the emulator it doesn't show the button enabled. Clicking next moves me to the third record and again the handler sets the prev button enabled but this time it does become enabled in the emulator. I'm grasping at straw here but do I need to invalidate and redraw or something?? I don't understand why such an elementary task is not working.
In XML:
<Button
android:id="#+id/btn_PrevLift"
...
android:enabled="false"
android:onClick="btn_PrevLiftClick" />
In the handler code:
private void UpdateNavButtonStatus(int z)
{
...
btn_Next.setEnabled(true);
btn_Prev.setEnabled(true);
....
}
No just to show you how little I know about what I'm doing how come when I look at the variable values in Eclipse debug I can't see the enabled property in any state???
More Info
Very odd to me at least. If I move from using XML defined event handlers to programatically defined as below it works great!!!???
btn_Nxt.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//Call helper methods etc...
}
});
I think I have it but not sure exactly why
So when I was trying to get a handle on my Button objects I was using the View.findViewById(etc). When I changed from XML to the programatically declared event handler I used ViewGroup.findViewById. Reverting back to xml if I use the ViewGroup I get a "different" handle that seems to work...????
Ok,so what you can do is declare a variable count and on the click f button next increment the value of count..and on the click of Back button decrement the value of count and give the condition that while count<0 the task of the back button is done else nothing
I have identified the problem and of course the problem is me. I was enabling the button and then the ListView was updating to the next record. Of course my enabled button was one behind so it appeared that when I clicked a second time the button suddenly enable but that is not the case. When I clicked a second time we moved to record three and showed button for record two which was enabled.
See my buttons were part of my ListView and new buttons were being drawn for each record. OOHHHH it makes so much sense now how everything was behaving.
Anyway I moved the buttons off the ListView layout so they remain constant as the user navigates through the records.
Right now in my android app a user presses a button to go to another activity, and then must press the back button on android to return to the previous activity. Can I have a button on my app and write code to go to the previous activity?
As the easiest way, you code write a code where your current activity is closed on a particular event, say on a button press. the method to be called is finish()
This way, when your current activity is finished, you are taken back to your previous activity.
Yes Ofcourse. : See the vogella article
I want to be able to press a button on my program and hold it down (without releasing) to increment a variable. Problem I am having right now is when I conduct the long button press it only runs once, until I release and press again.
First I want to find out if there is a way to do this without having to use the OnTouchListener, and just using the OnLongClick. Is there a way to check the value of the button? For example.. buttondown=true; Conduct a whileloop to increment until the button is released.
Second, I don't want the updates to be delayed, because the incremented value is being drawn as the user holds down the button.
Basically I am doing something like this:
btn_resume.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
..code..
return true;
}
});
OnLongClick will only be called once per press. It isn't going to work for your purpose.
If I understood your question correct this can be achieved using a OnLongClickListener.
Check http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)
OnTouchListener provides a more granular handling of touch events, e.g. KeyDown, KeyUp
I think you can use OnLongClickListener for increment/decrement. But once the long press is done for the button, the longpress has to be canceled or reset for the next long press of the same button.