The animation doesn't work properly when the activity resume - android

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
}
}

Related

How to enable back button wen setCancelable is set to false in Progress Dialog

I have a progress dialog, I have set progressDialog.setCancelable(false);,but i want to give user the option to use back button and go to previous activity.
Is it possible to enable back button when setCancableis set to false
Just finish your activity when the user presses the back button. Then they should return to the previous activity
#Override
public void onBackPressed() {
finish();
}

How would you disable user clicks while fragment is transitioning?

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

Reset seekbar to beginning on back activity

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.

How to prevent double-click on refresh button

I'm developing an android app where i've a refresh button in my action bar. This button call a function that re-open the same activity. This activity contains an asyncTask to load the content.
At the moment i'm encountering this problem. When i click on the refresh button it works fine, but if i click on the refresh button when the AsyncTask is still working (i've a progress bar to check the status) the app crashes.
The error that i receive is: NullPointerException
Is it possible to disable that button until the activity (and its AsyncTask) are completely loaded?
In your button's OnClickListener, from where you execute the AsyncTask, add this code:
button.setEnabled(false);
In onPostExecute() method of your AsyncTask, place this:
button.setEnabled(true);
If you also give the 'cancel' option to the user(i.e. if you have overridden the onCancelled() method in your AsyncTask), enable the button in onCancelled().
Edit 1:
Declare a boolean flag in your activity:
boolean menuButtonIsEnabled = true;
In your OnClickListener, set this flag to false:
menuButtonIsEnabled = false;
In onPostExecute() method of your AsyncTask:
menuButtonIsEnabled = true;
Override the onPrepareOptionsMenu(Menu) method in your activity:
#Override
public boolean onPrepareOptionsMenu (Menu menu){
super.onPrepareOptionsMenu(menu);
MenuItem button = menu.findItem(R.id.whatever_menu_button);
if(menuButtonIsEnabled){
button.setEnabled(true);
} else {
button.setEnabled(false);
}
return true;
}
In your onClickListener, the first thing you do is deactivate the button. This way it cannot be clicked again until you reactivate it :
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View target) {
target.setClickable(false);
target.setEnbled(false);
// Start your asynctask
}
}
in your AsyncTask.onPostResult(), you can reactivate the click on the button.
Nota: setClickable(false) prevents the button from reacting to clicks events, but setEnabled(false) also usually changes the appearance of the button.
You can try to disable the button from onPreExecute in AsyncTask and enable the button from onPostExecute in AsyncTask
You can check the status of your task.... On click of refresh button...
if(yourAsyncTaskObject != null && yourAsyncTaskObject.getStatus() != AsyncTask.Status.RUNNING){
then start your activity again here
}

Animation overriding onNavigateUp's default one

I am launching an About Activity with overridePendingTransition() right after it, so I can get an animation of the incoming Activity.
I want the Activity to perform an animation as well upon "leaving", so I have overriden onBackPressed() and it works ok.
The problem comes, as the About activity has the "Up navigation" enabled, on how to perform the animation when the "Up" navigation is tapped --instead of just Back button-- to return to previous activity.
I have tried
#Override
public boolean onNavigateUp() {
overridePendingTransition(R.anim.fadeinltr, R.anim.fadeoutltr);
return super.onNavigateUp();
}
but it does not work, because by the time the overridePendingTransition() method is called, there is no transition to override yet.
Any ideas?
I had the same problem and I solved in this way:
#Override
public boolean onNavigateUp(){
boolean x = super.onNavigateUp();
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
return x;
}

Categories

Resources