6
\$\begingroup\$

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();
    }
}
\$\endgroup\$

2 Answers 2

10
\$\begingroup\$

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 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 ){ in jump(){
  • There shouldn't be a space before the comma in this ,
\$\endgroup\$
2
\$\begingroup\$

On your splash screen, if the user presses the back button, the splash screen will finish. But login activity will open again, so you should handle the back button.

private boolean isBackPressed = false;

if (!isBackPressed) {
    startActivity(new Intent(this , LoginActivity.class));
    finish();
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {

        isBackPressed = true;
        finish();
        return true;
    } else {
        return super.onKeyDown(keyCode, event);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.