Search code examples
javaandroidandroid-activitysplash-screen

Splash Screen Problem on exiting from application when press exit button kindly help me plz


Hello Friends actually, I am a beginner app developer and I am facing this problem this problem hasn't any solution on youtube Actually when creating a splash screen for my app and making another activity first, the splash screen launched then the second activity(main page launched) but when I press the back button my app comes from the main page to the Splash screen have there any solution that when I press back I exit from the app and do not comes to the splash screen

Here is Splash screen Code

package com.example.a009hassan;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

        Thread threadname = new Thread(){
            public void run(){
                try {
                    sleep(800);
                }
                catch (Exception e){
                    e.printStackTrace();
                }
                finally {
                    Intent intentname = new Intent(MainActivity.this , MainActivity2.class);
                    startActivity(intentname);
                }
            }
        };threadname.start();
    }
}

Solution

  • In your thread, try this :

             Thread threadname = new Thread(){
                public void run(){
                    try {
                        sleep(800);
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    finally {
                        Intent intentname = new Intent(MainActivity.this , MainActivity2.class);
                        startActivity(intentname);
                        finish();
                    }
                }
            };threadname.start();