Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6878677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:45:11+00:00 2026-05-27T04:45:11+00:00

I have an animation which starts correctly the first time the fragment is displayed.

  • 0

I have an animation which starts correctly the first time the fragment is displayed. However after an orientation change, it won’t restart. The animation is an animation-list resource set as the background of an ImageView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View root = inflater.inflate(R.layout.fragment_lead_manual,
            container, false);
    final ImageView badgeEntryView = (ImageView) root
            .findViewById(R.id.manual_image);
    mAnimation = (AnimationDrawable) badgeEntryView.getBackground();
    return root;
}

@Override
public void onResume() {
    super.onResume();
    mAnimation.start();
}

@Override
public void onPause() {
    super.onPause();
    mAnimation.stop();
}

EDIT: I forgot to add that the animation is inside a tab, which makes things more difficult. However, I’ve figured out the problem and will add the answer below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:45:12+00:00Added an answer on May 27, 2026 at 4:45 am

    There are two cases that need to be solved, based on when the tab is created:

    1. FIRST the tab is created, and SECOND its Activity is attached to the Window
    2. FIRST the Activity is attached to the Window, and SECOND the tab is created

    Case one occurs if the tab is the first one displayed or during rotation. Case two occurs when the user switches to that tab because it’s not the first. Let’s handle each case separately:

    Case 1: a) Create tab b) Attach to window

    Calling AnimationDrawable.start() before it is attached to the window (i.e. inside onCreate() or onResume()) breaks the animation. As stated in the Android docs :

    It’s important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.

    It’s more difficult with Fragments, but basically the same. We override the method in the Activity and then call over to the Fragment:

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            final FragmentManager fm = getFragmentManager();
            ManualLeadFragment manualFragment = (ManualLeadFragment) fm
                    .findFragmentByTag(TAG_MANUAL);
            if (manualFragment != null) {
                manualFragment.startAnimation();
            }
        }
    }
    

    And then in the Fragment, implement startAnimation():

    void startAnimation() {
        mAnimation.start();
    }
    

    Case 2: a) Attach to window b) Create tab

    In this case, the call to onWindowFocusChanged() has already occurred and so the animation won’t start. So we still need to start it during onResume(), but slightly differently:

    @Override
    public void onResume() {
        super.onResume();
        if (isVisible()) {
            startAnimation();
        }
    }
    

    This calls into the same startAnimation() method as in Case 1, but because the Fragment is already attached to the Window, it can be called during onResume().

    Summary

    AnimationDrawable.start() can only be called when the Fragment is visible. Sometimes it is visible during onResume(), and the animation can be started at that point. Other times it is not yet visible at that time, and then the overridden onWindowFocusChanged() method is called when when it becomes visible, and the animation is started then.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this rectangle which after the first animation moves to a new position
I would like to have an animation effect which starts when people leave a
I have a problem with moving animation. I want to create animation which start
I have a little issue concerning an animation-effect which loads a certain div into
I have this code which should create a splash image with either no animation
I have a simple jquery animation on a div, which is inside a div,
I have an array randomAlphabets which contains CCSprite objects. I need to start animation
i am trying to have a horizontal progress bar, for which, it starts with
I have a function inside my .aspx.cs code which takes wuite a long time
I have an object animating, which I would like to start fading out, after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.