user3685089 February 2016

Android studio splash screen loops my activity - threading

first of all I´m new to Android. I have found a piece of code to make splash on app startup. When I integrated with my project I get infinitive loop on my login/second activity. I´m guessing I do something wrong with the threading.

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;



public class WelcomeSplashLogo extends Activity{

public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

Thread splashTread;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome_splash_logo);
    StartAnimations();


}

private void StartAnimations() {
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
    anim.reset();
    LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
    l.clearAnimation();
    l.startAnimation(anim);

    anim = AnimationUtils.loadAnimation(this, R.anim.translate);
    anim.reset();
    ImageView iv = (ImageView) findViewById(R.id.splash);
    iv.clearAnimation();
    iv.startAnimation(anim);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                // Splash screen pause time
                while (waited < 3500) {
                    sleep(100);
                    waited += 100;
                }
                Intent intent = new Intent(WelcomeSplashLogo.this, WelcomeSplashLogo.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                WelcomeSplashLogo.this.finish();
            } catch (InterruptedException e) {
                   

Answers


user5716019 February 2016

It just because of the below lines:

Intent intent = new Intent(WelcomeSplashLogo.this, WelcomeSplashLogo.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            WelcomeSplashLogo.this.finish();

You are calling Same Activity i.e. WelcomeSplashLogo.class.


Mohit February 2016

Instead use Handler to queue and run your task after the specified amount of time elapses and go to next screen...

Handler handler;
Runnable runnable;    
runnable = new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(WelcomeSplashLogo.this, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            finish();
        }
    };
handler = new Handler();
handler.postDelayed(runnable, 3000);  // delay time...


Jörn Buitink February 2016

Personally, I don't think it is a great idea to make the user wait for 3.5 seconds if you can show your main app sooner.

I made some comments to your code:

splashTread = new Thread() {
    @Override
    public void run() {
        try {
            int waited = 0;
            // Splash screen pause time
            while (waited < 3500) {    // why loop? sleep(3500) would do it, too.
                sleep(100);
                waited += 100;
            }
            /*
            Intent intent = new Intent(WelcomeSplashLogo.this, WelcomeSplashLogo.class);  // You call your SplashLogo Activity again. 
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            WelcomeSplashLogo.this.finish();   // this is called in finally block already, therefore it is not needed here
            */
        } catch (InterruptedException e) {
            // do nothing
        } finally {
            WelcomeSplashLogo.this.finish();

            Intent intentGoToHomeActivity = new Intent(WelcomeSplashLogo.this, LoginActivity.class);
            WelcomeSplashLogo.this.startActivity(intentGoToHomeActivity);
        }

    }
};
splashTread.start();


Abhinaw Kumar February 2016

Use handler

 new Handler().postDelayed(new Runnable() {

                /*
                 * Showing splash screen with a timer. This will be useful when you
                 * want to show case your app logo / company
                 */

                @Override
                public void run() {
                    // This method will be executed once the timer is over
                    // Start your app main activity
                    Intent i = new Intent(WelcomeSplashLogo.this, WelcomeSplashLogo.class);
                    startActivity(i);

                    // close this activity
                    finish();
                }
            }, 3000);

Post Status

Asked in February 2016
Viewed 2,214 times
Voted 7
Answered 4 times

Search




Leave an answer