Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
How to share object for all app's activities

I have an animation that I want to apply on every button in my app. So I don't want to call AnimationUtils.loadAnimation() in every activity onCreate method. I would like to call this method only one time when the app is launched to initialize my animation object, and then get it (with a getter) in my different activities. I'm new in Android programming, I was planning to use a Singleton pattern but it looks like to be "unsafe" in Android related to this article and other Stackoverflow pages. (https://programmerr47.medium.com/singletons-in-android-63ddf972a7e7)

Is there anyway to create my Animation at app launching and share it between every activity ? And does it worth to do some optimization ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I'd suggest extending the android Button/AppCompatButton class, adding the functionality you want to the extended class and using that button everywhere in your app much easier that way and probably the most correct way,

For example:

AnimatedButton.java:

package com.example.myapplication;

import android.content.Context;
import android.util.AttributeSet;

public class AnimatedButton extends androidx.appcompat.widget.AppCompatButton {
    public AnimatedButton(Context context) {
        super(context);
        createAnimation();
    }

    public AnimatedButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AnimatedButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void createAnimation() {
        // here create the animation and call
        // setAnimation([YOUR_ANIMATION_HERE]);
        // now you can simply call customButton.animate();
        // from anywhere in the code that uses the button and it should work
    }
}

In your xml where you want to use the button:

<com.example.myapplication.AnimatedButton
            android:id="@+id/btn_animate"
            android:layout_width="180dp"
            android:layout_height="80dp"
            android:text="Animate" />
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda