I have a seekbar in my activity. In the button event when I go to another activity and then use back button, the values of the seekbar are set where the user left it. I would like to reset the seekbar when the user comes back to the seekbar activity. How can I achieve it? Thanks
public void onClick(View v) {
if (seek.getProgress > 0) {
// do something
}
}
simply reset the seek bar in onPause or onResume of your SeekBarActivity, i.e. :
#Override
protected void onPause() {
seek.setProgress(0);
super.onPause();
}
This will be called when you have started another activity and the current one is paused.
Related
I have a PreferenceActivity with an EditTextPreference. I can easily detect positive button press using OnPreferenceChangedListener. However, the listener is never called when the user presses the negative button.
Is there anyway to detect it?
Thanks.
You cannot detect it unless you override the onDialogClosed(...) in EditTextPreference (assuming that you use the old and now deprecated framework version of the preferences):
public class EditTextPreferenceNegative extends EditTextPreference {
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult); // this will notify the change listener for positive results
if (!positiveResult) {
// do something with your negative result
}
}
}
Note, however, that this will be called even if the dialog is closed without pressing the negative button. If you want to detect only the negative button, you can override the onClick(...) method which is called when either of the buttons is clicked.
There are two buttons, button A starts another activity inside its onClickListener using simple Activity.startActivity() method, button B does some other work inside its onClickListener.
When I click button B and immediately after button A, then new activity is started, onPause() lifecycle event for old activity is fired, but also onClick event for second button is fired, but after Activity.onPause() which leads to some state inconsistencies in my app.
Is there any way to prevent touch/click events from being delivered after onPause() without using isPaused flag?
**Edit:**My code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonA = (Button) findViewById(R.id.activity_button);
buttonA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, TestActivity.class));
}
});
Button buttonB = (Button) findViewById(R.id.log_button);
buttonB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("TEST", "onClick");
}
});
}
#Override
protected void onPause() {
super.onPause();
Log.e("TEST", "onPause");
}
}
Basically if you are fast enough and click button B right after button A then B's onClick is fired after onPause.
In OnClickListener of button A, disable the button b.
Button.setEnabled(false);
Just enable the button at the of A's onClickListener or at onResume depending on your requirements.
I know this question is very old and far far away from being active. I came across this question via a blog.
A simple solution which I can think off is maintaining a flag globally in Activity A and setting it immediately inside onClick of Button A. This flag can be reset in onResume of Activity A.
This flag should be used at the top of onClick handler and all the clicks of all the views must be ignored. Of course, this requires that there is a single onClick method.
private boolean isOnClickIgnored= false;
#Override
public void onResume() {
super.onResume();
isOnClickIgnored = false;
}
#Override
public void onClick(View v) {
super.onClick(v);
if(isOnClickIgnored){
return;
}
switch (v.getId()){
case R.id.btnA:
isOnClickIgnored = true;
// Do button a things like opening Activity B
break;
case R.id.btnB:
// Do button b things
break;
case R.id.btnZ:
// Do button z things
break;
}
}
This whole concept can be easily solved using ViewModels + lifecycle-aware LiveDatas where LiveData expose events to UI-layer only when it's allowed.
as title says, how to make my ripple effect on ImageButton (ripple effect assigned to this ImageButton) activated on backpressed() without back to the previous activity
with a bit of logic you could achieve that easily. what i thought of is that you have a method lets call it doSomething();
boolean onBackpressed = false;
private void doSomthing() {
if(onBackpressed){
finish();
}else{
// do anything else that the button wants to do;
}
}
now onBackpressed() you could do this
#Override public void onBackpressed() {
onBackpressed = true;
myImageButton.performClick(); // myImageButton.callOnClick()
}
P.S: that i didn't call super.onBackpressed(); so we can control the back press without existing the app.
of course your imageButton click listener calls doSomething() method.
I want my fragment to not receive any clicks on the views while the fragment transition animation is not yet finished. It is just a simple fade. But things get wonky when I immediately press any view while the next fragment is fading in.
Any thoughts how to achieve this?
This is actually used in my own app. The idea is very simple, it just works, but needs quite a lot of additional coding.
The idea is very simple, use a boolean variable to maintain whether the screen should be locked, let's call it screenLocked. I do not actually block the click, but let the click do nothing.
For those actions which takes time, set screenLocked to true before start working, and set it back to false when the task is finished. Also you have to add checking on screenLocked before any action is done.
Another difficulty of the this method is that you need to have clear end point of your tasks. Using Fragment transition as an example, if the backstack is poped, there has no actual callback notifying you, for this case. To handle this, I would set another flag releaseOnResume before starting Fragment transition, and in onResume, I would use this flag to check if I should set screenLocked back to false.
Other solutions I have tried but not used:
Before I settled with the method I just mentioned, I have tried setEnabled, setClickable, or any UI based blocking, e.g. add a FrameLayout on top and capture all touch events.
These methods are not bad, especially given that they are easy to implement.
The only problem is that, onClick events can be queued due to double tapping, when you are handling the first onClick event, actually there could be another one queued up, even if you do any UI changes immediately to block any further clicks, you can't stop the next onClick event to come because it is queued already.
Hope this helps.
I use a countdown timer.
I manage this through the ontouch listener.
I create a method that manages the creation of the timer. I call it in the ontouch event. I use two methods (this is optional, but good for extensibility) to handle button enabling and disabling. I then use these methods with the timer to enable and disable the button.
See my code snippet.
In oncreate:
#Override protected void onCreate(Bundle savedInstanceState) {
/.../
button.setOnTouchListener(new View.OnTouchListener() {
#Override public boolean onTouch(View v, MotionEvent event) {
disableButton(button);
countDwn1();
/... time to do whatever you need..
// custom methods...
fragment = new MyFragAddFragment();
replaceFragment(fragment);
return false;
}
});
Methods:
public void countDwn1() {
CountDownTimer countDownTimer = new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
enableButton(button);
}
}.start();
}
public void disableButton(Button button) {
button.setEnabled(false);
}
public void enableButton(Button button) {
button.setEnabled(true);
}
You can extend this method to include passing the button as a parameter into the timer, for extensibility.
In the end I used something like this. I created a parent class for all my fragments and overriden the OnCreateAnimation method which is called on every animation.
#Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
//Check if the superclass already created the animation
Animation anim = super.onCreateAnimation(transit, enter, nextAnim);
//If not, and an animation is defined, load it now
if (anim == null && nextAnim != 0) {
anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);
}
//If there is an animation for this fragment, add a listener.
if (anim != null) {
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
isAnimationFinished = false;
}
#Override
public void onAnimationEnd(Animation animation) {
isAnimationFinished = true;
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
return anim;
}
The isAnimationFinished variable is a public variable that can be used by the calling activity and the child classes
All
I am developing an android app, the first activity is the MenuActivity, there is a button in the MenuActivity, I would like to make the button slide into the screen with animation when the menuActivity startup, so I write the following code in the onResume method of the MenuActivity.
#Override
protected void onResume(){
super.onResume();
Animation animation = AnimationUtils.loadAnimation(MenuActivity.this,
android.R.anim.slide_in_left);
animation.setInterpolator(new OvershootInterpolator());
button.startAnimation(animation);
}
When I run the app, the MenuActivity show up for the first time, the button can slide in the screen with the animation properly, and then I click on the button, it will start another activity called ContentActivity, and then I click the back button to close the ContentActivity, the MenuActivity show up again, but for the second time the button doesn't slide in the screen with animation any more, the button just show up in the screen immediately with not translation animation.
Can anyone help me out from this issue??
Override the onWindowFocusChanged() method and move your animation code there instead.
public void onWindowFocusChanged(boolean hasFocus)
{
if(hasFocus)
{
//your code here
}
}