Start Splash but after the app is closed

0

Splash

    public class Splash extends AppCompatActivity {
    // Set the duration of the splash screen
    private static final long SPLASH_SCREEN_DELAY = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        TimerTask task = new TimerTask() {
            @Override
            public void run() {

                // Start the next activity
                Intent mainIntent = new Intent().setClass(
                        Splash.this, MainActivity.class);
                startActivity(mainIntent);

                // Close the activity so the user won't able to go back this
                // activity pressing Back button
                finish();
            }
        };

        // Simulate a long loading process on application startup.
        Timer timer = new Timer();
        timer.schedule(task, SPLASH_SCREEN_DELAY);
    }
}

Manifest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true">
    <activity
        android:name=".Splash"
        android:theme="@style/ThemeGallo.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />/<!--Quita ActionBar/ToolBar-->
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity">

    </activity>
</application>

I hope and you can help me, Thanks.

    
asked by Javier fr 09.04.2017 в 21:33
source

1 answer

1

Do the intent this way, getting the context by getApplicationContext() :

  TimerTask task = new TimerTask() {
        @Override
        public void run() {

            // Start the next activity
            //Intent mainIntent = new Intent().setClass(Splash.this, MainActivity.class);
             Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
             startActivity(mainIntent);

            // Close the activity so the user won't able to go back this
            // activity pressing Back button
            finish();
        }
    };

// Simulate a long loading process on application startup.
Timer timer = new Timer();
timer.schedule(task, SPLASH_SCREEN_DELAY);

link

You must ensure that MainActivity does not have any problems since it could be another cause that your application is closed.

    
answered by 10.04.2017 в 06:21