patternjavaModerate
SplashScreen class
Viewed 0 times
splashscreenclassstackoverflow
Problem
I want to know about best practice for this
SplashScreen class:public class SplashScreen extends Activity {
// time out to splash screen
private static int SPLASH_TIME_OUT = 2000;
private Runnable mjumpRunnable;
private Handler mHandler;
//image view to validae logo
ImageView validatelogo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
validatelogo = (ImageView) findViewById(R.id.imgvalidatelogo);
validatelogo.setVisibility(View.VISIBLE);
final Animation fadeouteffect = AnimationUtils.loadAnimation(this , R.anim.fadein);
validatelogo.setAnimation(fadeouteffect);
mjumpRunnable = new Runnable() {
@Override
public void run() {
jump();
}
};
mHandler = new Handler();
mHandler.postDelayed(mjumpRunnable , SPLASH_TIME_OUT);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
jump();
}
return super.onTouchEvent(event);
}
void jump(){
if(isFinishing())
return;
startActivity(new Intent(this , LoginActivity.class));
finish();
}
}Solution
So you want to follow good practices. There are quite a few things to pick on here.
Limit access as much as possible
Try to make member fields
For example, why isn't this variable private?
Aim for immutability
Immutable things are easy to work with:
as they never change,
there is no window of vulnerability,
you can trust immutable things from the time they are constructed forever.
Even if true immutability is not achievable,
it's good to take steps in that direction.
A good first step is to try to make member fields
For example, these variables could easily be
Limit scope as much as possible
By limiting the scope where a variable is visible,
you make the variable easier to understand,
and reduce the probability of accidental modifications.
For example, these member variables are only used inside one method,
so they should be declared there (converting to local variables from members):
Naming
it should be
Layout
There are many pointless blank lines,
especially at the end of
Formatting
There are multiple formatting issues here:
That is:
Limit access as much as possible
Try to make member fields
private first, and relax access as needed.For example, why isn't this variable private?
ImageView validatelogo;Aim for immutability
Immutable things are easy to work with:
as they never change,
there is no window of vulnerability,
you can trust immutable things from the time they are constructed forever.
Even if true immutability is not achievable,
it's good to take steps in that direction.
A good first step is to try to make member fields
final.For example, these variables could easily be
final:private static int SPLASH_TIME_OUT = 2000;
private Handler mHandler;Limit scope as much as possible
By limiting the scope where a variable is visible,
you make the variable easier to understand,
and reduce the probability of accidental modifications.
For example, these member variables are only used inside one method,
onCreate,so they should be declared there (converting to local variables from members):
private Runnable mjumpRunnable;
private Handler mHandler;
ImageView validatelogo;Naming
mjumpRunnable doesn't conform to Android naming conventions,it should be
mJumpRunnable (with capitalized "J").Layout
There are many pointless blank lines,
especially at the end of
onCreate.Formatting
There are multiple formatting issues here:
void jump(){
if(isFinishing())
return;
startActivity(new Intent(this , LoginActivity.class));
finish();
}That is:
- Inconsistent indentation
- It's recommended to use braces always, even with single-statement
if
- There should be a space between
){injump(){
- There shouldn't be a space before the comma in
this ,
Code Snippets
ImageView validatelogo;private static int SPLASH_TIME_OUT = 2000;
private Handler mHandler;private Runnable mjumpRunnable;
private Handler mHandler;
ImageView validatelogo;void jump(){
if(isFinishing())
return;
startActivity(new Intent(this , LoginActivity.class));
finish();
}Context
StackExchange Code Review Q#101840, answer score: 10
Revisions (0)
No revisions yet.