Search code examples
androidandroid-activityactivity-finish

How to close app in activity


My app start with splash screen. Splash screen visible for 5 seconds then menu activity start. But when splash screen on display and I press back or home button app go in background but after few seconds its come on foreground automatically with menu activity. I want if user press home or back key app close permanently. Here is what I have tried so far.

Splash Screen Activity-

public class SplashScreen extends Activity 
{
    /** Called when the activity is first created. */
    TimerTask task;
    Intent objIntent, intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.splash);
       UtilClass.playing = true;
       objIntent = new Intent(SplashScreen.this, PlayAudio.class);
       startService(objIntent);
       new Handler().postDelayed(csRunnable2, 5000);  
       }

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

   public void onBackPressed()  
    {    
        objIntent = new Intent(this, PlayAudio.class);
        stopService(objIntent);
        finish();
        return;  
    }  

   @Override
    protected void onPause() {
        super.onPause();
        objIntent = new Intent(this, PlayAudio.class);
        stopService(objIntent);
        finish();
    }
}

You can see in onPause and onBackPressed I am closing app. But its start with menu activity after few seconds.


Solution

  • what suitianshi suggests is

        public class SplashScreen extends Activity
    {
        /** Called when the activity is first created. */
        TimerTask task;
        Intent objIntent, intent;
        Handler handler;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
            UtilClass.playing = true;
            objIntent = new Intent(SplashScreen.this, PlayAudio.class);
            startService(objIntent);
            handler = new Handler();
            handler.postDelayed(csRunnable2, 5000);
        }
    
        Runnable csRunnable2 = new Runnable()
        {
            @Override
            public void run()
            {
                intent = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        };
    
        public void onBackPressed()
        {
            objIntent = new Intent(this, PlayAudio.class);
            stopService(objIntent);
            finish();
            return;
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            objIntent = new Intent(this, PlayAudio.class);
            stopService(objIntent);
            finish();
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            handler.removeCallbacks(csRunnable2);
        }
    }