1

I have a very simple first activity in my app, which is a "splash" activity. It only displays a image and after a small animation it launch the main menu activity of my app.

The problem is that this first splash activity takes a huge amount of time to being displayed if the app is not in memory. I mean, if i completly destroy the app from the memory and start it from scratch, whithout being in recently app list of the SO.

It takes 3-4 seconds to display the content of the activity. In these 3-4 seconds i can see a dark screen (not 100% black) with the default black background style of the system and not the Color.BLACK background color of the activity, so the time spent in the loading of the first app is spent before the activity is displayed.

Why is taking so much to start? Maybe is for my dependencies at gradle file?

These are my gradle dependencies:

dependencies {
    compile 'com.google.android.gms:play-services-ads:9.0.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}

This is the simple splash activity. I tryed also commenting AdManager.getInstance().initAd(this); to see if it solves the problem. It takes a little less time to start if that line is commented. That line initializes admob. But i still have the problem.

public class Splash extends Activity {
    private AnimatorSet set;
    private boolean backPressed;

    public void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);

        AdManager.getInstance().initAd(this);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);      
        int sh=dm.heightPixels;
        int sw=dm.widthPixels;

        ImageView splash = new ImageView(getApplicationContext());
        splash.setLayoutParams(new LayoutParams((int) (sw*0.9), LayoutParams.WRAP_CONTENT));
        splash.setImageResource(R.drawable.logo);

        //contenemos la ImageView header dentro de un layout para asi cambiar el color de fondo y que sea el mismo que el de la imagen.
        LinearLayout headerll = new LinearLayout(this);
        RelativeLayout.LayoutParams headerllParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, sh/20);
        headerll.addView(splash);
        splash.setPadding(10, 10, 10, 10);
        headerll.setLayoutParams(headerllParams);
        headerll.setBackgroundColor(Color.BLACK);
        headerll.setGravity(Gravity.CENTER);        

        setContentView(headerll);

        set = new AnimatorSet();
        ObjectAnimator in = ObjectAnimator.ofFloat(splash, "alpha", 0f, 1f);
        in.setDuration(2000);
        in.setInterpolator(new AccelerateInterpolator());

        ObjectAnimator delay = ObjectAnimator.ofFloat(splash, "alpha", 1f, 1f);
        delay.setDuration(1000);

        ObjectAnimator out = ObjectAnimator.ofFloat(splash, "alpha", 1f, 0f);
        out.setDuration(2000);
        out.setInterpolator(new AccelerateInterpolator());

        set.addListener(new AnimatorListener() {
            @Override
            public void onAnimationCancel(Animator animation) {}
            @Override
            public void onAnimationEnd(Animator animation) {
                finish();
                Intent i = new Intent(Splash.this, MainActivity.class);
                startActivity(i);
            }
            @Override
            public void onAnimationRepeat(Animator animation) {}
            @Override
            public void onAnimationStart(Animator animation) {}
        });

        set.playSequentially(in,delay,out);
        set.start();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        backPressed=true;
        set.cancel();
    }               
}
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

1 Answers1

0

According to similar questions the problem might be in Android Studio. This bug is only in debug mode and does not affect your release APK. Try this as temporary solution:

Settings → Build, Execution, Deployment → Instant Run and uncheck Enable Instant Run
pr0gramist
  • 8,305
  • 2
  • 36
  • 48