-1

I'm a Android beginer. I have made a SplashScreen changing img picture inside ImageView evry 2 seconds. Last image is shown two times in row to avoid dissaper it immediately before move to MainActivity. So It works somehow - pictures are changing and finally it takes user to MainActivity where I have just one button which does nothing(just to have enything visible there for test). Theoretically everything is fine but each 8 seconds MainActivity is probably reload again. I can see that becouse Button is jumping 1px down and up.

Could you please take a look on my code and answer the question Why? Thank You!

public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_splash_screen);
    handler.postDelayed(runnable, 0);

}

@Override
protected void onDestroy() {
    super.onDestroy();
}

int[] imageArraySplashScreen = { R.drawable.pierwszy, R.drawable.drugi, R.drawable.trzeci, R.drawable.trzeci};
Handler handler = new Handler();
Runnable runnable = new Runnable(){
    int i = 0;
    ImageView splashImageView;

    public void run() {
        splashImageView = findViewById(R.id.idSplashScreenImageView);
        splashImageView.setImageResource(imageArraySplashScreen[i]);
        i++;
        if (i>imageArraySplashScreen.length-1){
            i=0;
            Intent splashScreenIntent = new Intent(SplashScreen.this, MainActivity.class );
            startActivity(splashScreenIntent);
            finish();
        }
        handler.postDelayed(this, 2000);
    }
};

}

TSWR22
  • 3
  • 2

2 Answers2

-1

Try

handler.removeCallbacks(runnable);

in onPause()

Hello World
  • 740
  • 5
  • 18
-1

You need to post handler only if images left to show. Just Change your code Like,

Handler handler = new Handler();    
Runnable runnable = new Runnable() {
        int i = 0;
        ImageView splashImageView;

        public void run() {

            splashImageView.setImageResource(imageArraySplashScreen[i]);
            i++;
            if (i > imageArraySplashScreen.length - 1) {
                i = 0;
                Intent splashScreenIntent = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(splashScreenIntent);
                finish();
            } else {
                handler.postDelayed(this, 2000);
            }

        }
    };
Abhay Koradiya
  • 2,068
  • 2
  • 15
  • 40