Here's my android splash screen. I am trying to figure out a better way to write it. It just moves the logo up to the center of the screen. It's my third time creating a splash screen for an android app. I am just trying to figure out the best way to write more optimized code.
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
final public class Splash extends AppCompatActivity {
private final static int timeout = 3000;
final static Handler handler = new Handler();
static ImageView logo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logo = findViewById(R.id.companylogo);
Animation animation = AnimationUtils.loadAnimation(Splash.this, R.anim.myanim);
logo.startAnimation(animation);
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent splashscreen = new Intent(Splash.this, Home.class);
startActivity(splashscreen);
}
}, timeout);
}
}
1 Answer 1
This code looks reasonably clean and seems to accomplish what it's intended to do. Some minor nitpicks can still be improved here and there:
You're not consistent in visibility modifier ordering between class declaration and member declarations. It should either be
public final class
orfinal private static int timeout
.
Most Java conventions that I've seen default to thepublic static final
ordering.handler
andlogo
are currently package-private. It's very unusual for that visibility to be necessary and I think setting these toprivate
if at all possible would be better. (I'm not recommendingprotected
, because the class can not be extended.)static final
members are usually named inSNAKE_CAPS
(also known as shout-case).the id
companylogo
might be better off namedcompanyLogo
to follow camel case conventions.myanimation
doesn't tell us anything about the animation. A better name would besplashScreenAnimation
orlogoAnimation