Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Cómo compartir objetos para todas las actividades de la aplicación

Tengo una animación que quiero aplicar en cada botón de mi aplicación. Así que no quiero llamar a AnimationUtils.loadAnimation() en cada actividad en el método Crear. Me gustaría llamar a este método solo una vez cuando se inicia la aplicación para inicializar mi objeto de animación y luego obtenerlo (con un getter) en mis diferentes actividades. Soy nuevo en la programación de Android, estaba planeando usar un patrón Singleton pero parece ser "inseguro" en Android relacionado con este artículo y otras páginas de Stackoverflow. ( https://programmerr47.medium.com/singletons-in-android-63ddf972a7e7 )

¿Hay alguna forma de crear mi Animación al iniciar la aplicación y compartirla entre cada actividad? ¿Y vale la pena hacer alguna optimización?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Sugeriría extender la clase Android Button / AppCompatButton , agregar la funcionalidad que desea a la clase extendida y usar ese botón en todas partes de su aplicación mucho más fácil de esa manera y probablemente la forma más correcta,

Por ejemplo:

BotónAnimado.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 } }

En tu xml donde quieres usar el botón:

 <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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!