문제

When i run the app, SplashScreen blinks for a fraction of second on the screen and disappears completely for 3 seconds. After completing 3 sec MainActivity launches. Question is how to display my splash screen with out disapearing?

public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);

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

        @Override
        public void run() {
            startActivity(new Intent(SplashScreen.this, MainActivity.class));

        }
    }, SPLASH_TIME_OUT);
    SplashScreen.this.finish();
}

@Override
public void onBackPressed() {
    SplashScreen.this.finish();
    super.onBackPressed();
}}

MANIFEST FILE

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.simbotix.guardianonthego.SplashScreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.simbotix.guardianonthego.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
도움이 되었습니까?

해결책

Change this

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

        @Override
        public void run() {
            startActivity(new Intent(SplashScreen.this, MainActivity.class));

        }
    }, SPLASH_TIME_OUT);
    SplashScreen.this.finish();

to

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

        @Override
        public void run() {
             startActivity(new Intent(SplashScreen.this, MainActivity.class));
              SplashScreen.this.finish();
        }
    }, SPLASH_TIME_OUT);

Also no need to finish Activity in onBackPressed

@Override
public void onBackPressed() {
    SplashScreen.this.finish(); // Remove this
    super.onBackPressed();
}

다른 팁

       new Handler().postDelayed(new Runnable() {    
        @Override
        public void run() {
            startActivity(new Intent(SplashScreen.this, MainActivity.class));
            SplashScreen.this.finish();

        }
    }, SPLASH_TIME_OUT);

SplashScreen.this.finish(); line of code finish your SplashActivity and you need to execute this after 3 seconds that's why it is in handler thread and executed after 3 seconds.

Simply just do not finish your SplashScreen before starting MainActivity. Just remove the SplashScreen.this.finish(); line from your SplashScreen class.

 public void run() {
                startActivity(new Intent(SplashScreen.this, MainActivity.class));
                finish()
            }
 }, SPLASH_TIME_OUT);

Instead of

public void run() {
            startActivity(new Intent(SplashScreen.this, MainActivity.class));

        }
    }, SPLASH_TIME_OUT);
    SplashScreen.this.finish();

Quick Fix :

remove

SplashScreen.this.finish();

Change like,

public void run() {
        startActivity(new Intent(SplashScreen.this, MainActivity.class));
        finish();
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top