Question

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.

Was it helpful?

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);
    }
}

OTHER TIPS

As, Chris mentioned, Declare your HANDLER globally as,

 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);
}

Here in this Line,

        handler.postDelayed(csRunnable2, 5000);

You are calling your "csRunnable2", after 5 seconds, So, it will come up after 5 seconds until unless,

=> You destroy your instance of handler class completely or ,

=> Make all the references Null.

So, When ever you are closing or pausing your activity(as in onBackPressed(), onPause(), onStop(), onDestroy() etc.,.) make sure to call :

 handler.removeCallbacksAndMessages(null);

before you start next Activity.

This will makes null of all Handler instances.

For eg., in your code you can call,

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

@Override
protected void onPause() {
    super.onPause();
    handler.removeCallbacksAndMessages(null);
    objIntent = new Intent(this, PlayAudio.class);
    stopService(objIntent);
    finish();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top