0

I have activity that contain fragment. This fragment have a lottie animation with lottie_loop="false", that means, once the animation finish first loop , the animation will be and.

I want to listen for this event(animation end) in activity that contain this fragment, but some this wrong with my code, and I have white screen.

I created interface for listen to even , and this is my code:

Fragment with lottie animation:

public class EntryFragmentAnimation extends Fragment {

    private View view;
    private LottieAnimationView mLavTwoHearts;
    private boolean isAnimationEnd = false;
    private OnAnimationEndListener iOnAnimationEndListener;

    public interface OnAnimationEndListener {
        void onAnimationEnd(boolean isAnimationEnd);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_entry_animation, container, false);
        initView(view);
initAnimation();
        return view;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        try {
            iOnAnimationEndListener = (OnAnimationEndListener) getActivity();
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement OnAnimationEndListener");
        }
    }

    private void initView(View view) {
        mLavTwoHearts = view.findViewById(R.id.lavTwoHearts);
    }

    private void initAnimation() {
        mLavTwoHearts.playAnimation();
        mLavTwoHearts.addAnimatorListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                isAnimationEnd = true;
                iOnAnimationEndListener.onAnimationEnd(isAnimationEnd);

            }
        });
    }
}

And an activity

public class LoginActivity extends AppCompatActivity implements EntryFragmentAnimation.OnAnimationEndListener {

    private boolean isAnimationEnd = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setEntryScreen();
        listenToAnimationEnd();
    }

    private void setEntryScreen(){
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container_login_fl, new EntryFragmentAnimation())
                .commit();
    }

    private void listenToAnimationEnd(){
        while (!isAnimationEnd){
            Log.d(TAG, "listenToAnimationEnd: Animation playing");
        }
        Log.d(TAG, "listenToAnimationEnd: animation end");
    }

    @Override
    public void onAnimationEnd(boolean isAnimationEnd) {
        this.isAnimationEnd = isAnimationEnd;
    }
}

While running the app , only white screen appear and in logcat running endless log with Animation playing

Michael
  • 429
  • 5
  • 22
  • why do you want to listen to endless log? Why don't you just mark the starting point in onAnimationStart and listen till animationEnd? – Gautam Jan 31 '20 at 13:05
  • I do not want to listen to an endless log, this log is just for me, my goal, is to listen to the animation end, report to the activity, and make actions on activity – Michael Jan 31 '20 at 13:07

1 Answers1

0

Instead of a listener I would suggest you to better use a ViewModel. You only need to create ViewModel class and create its instance in fragment but using the activity scope so that it will be available for all the fragment contained within the activity including activity itself.

In your fragment create a Shared ViewModel instance like below:

activity?.let {
    sharedViewModel = ViewModelProviders.of(it).get(SharedViewModel::class.java)
}

Once the animation ends update the the ViewModel

sharedViewModel?.onAnimationFinished()

Note: Inside you ViewModel class, have any live data member which is being obeserved by your Activity and then just update the variable within the function.

In the activity we just need to create instance of our ViewModel and observe the required data like this

val sharedViewModel = ViewModelProviders.of(this).get(SharedViewModel::class.java)

sharedViewModel.animationEndEvent.observe(this, Observer {
    it?.let {
        // do some thing
    }
})
Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20