Akshay Damle
Akshay Damle

Reputation: 1250

How to make the application close if splash activity is closed in Android

I have a splash activity that is displayed for 2 seconds before opening the main activity. If the user presses the back button while the splash activity is being displayed, the splash activity closes. But a short time later, the main activity [which was triggered by the splash activity] opens up.

I don't want this to happen. I want the entire application to close if the back button is pressed while the splash screen is being displayed. How do I accomplish this?

Edit: below is my code for splash activity:

    public class Splash2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // fading transition between activities
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);

        setContentView(R.layout.activity_splash2);
        Thread timer = new Thread() {
            public void run() {
                try {
                    sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    Intent open = new Intent(
                            "com.example.puzzletimer.HOMESCREEN");
                    startActivity(open);
                }
            }
        };
        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}

Upvotes: 4

Views: 672

Answers (3)

Melquiades
Melquiades

Reputation: 8598

Another way to do it is with runnable:

  1. Declare Handler and Runnable in your Splash2 activity:

    private Handler handler;
    private Runnable startMain;
    
  2. In onCreate(), initialize them and set the runnable to fire in 1.5 secs:

    handler = new Handler();
    startMain = new Runnable() {
        @Override
        public void run() {
            Intent open = new Intent("com.example.puzzletimer.HOMESCREEN");
            startActivity(open);
        }
    };      
    
    handler.postDelayed(startMain, 1500);
    
  3. Override onBackPressed() and simply cancel runnable:

    @Override
    public void onBackPressed() {
        handler.removeCallbacks(startMain);
        super.onBackPressed();
    }
    

That's it. If back key is pressed, it cancels runnable and your homescreen won't run.

Upvotes: 0

Gopal Gopi
Gopal Gopi

Reputation: 11131

Just add this code to your SplashActivity...

@Override
public void onBackPressed() {
    super.onBackPressed();
    android.os.Process.killProcess(android.os.Process.myPid());
}

or maintain one flag to determine to start Activity or not in Thread...

public class Splash2 extends Activity {

    private volatile boolean interrupt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // fading transition between activities
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);

        setContentView(R.layout.activity_splash2);
        Thread timer = new Thread() {
            public void run() {
                try {
                    sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if (!interrupt) {
                        Intent open = new Intent(
                                "com.example.puzzletimer.HOMESCREEN");
                        startActivity(open);
                    }
                }
            }
        };
        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        interrupt = true;
    }
}

Upvotes: 2

Kanwaljit Singh
Kanwaljit Singh

Reputation: 4377

Try this-

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}

Upvotes: 0

Related Questions