1
votes

Hi in my android app I included a splash screen after this activity whenever I pressed the back button it is going to the previous pages of MainActivity. But I needed to exit from application if user press back button from MainActivity. But now currently mainactivity is not the start activity and splash screen is the activity. So i checked some methods and I saw

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

this will clear the previous activities.. But in MainActivty why I need an intent.. I gave

finish();

in every intent of each activity so the activity will be cleared. But that is affecting my app's entire structure .. whenever user press back button app is going to home page. So any one suggest a solution.. so that I can exit directly from MainActivity if user presses a back button.

this is my backpress code for mainActivity

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub  
    super.onBackPressed();    


    finish();

}

and my splashscreen

WebView wv;
     // Splash screen timer
    private static int SPLASH_TIME_OUT = 4000;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_flash);

        wv = (WebView) findViewById (R.id.webView1);
        wv.setHorizontalScrollBarEnabled(false);



        wv.loadUrl("file:///android_asset/Finally-320.gif");


        new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */


            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start app main activity
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);

                // close this activity
                finish();
            }


        }, SPLASH_TIME_OUT);

}
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub

        super.onBackPressed();
            finish();

}

}
2
use intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); for calling MainActivity from SplashScreen , and don't call finish method on other activity, if you want call finish, you need handle all onBackPress - Shayan Pourvatan
but what is the use of adding in splash screen.. - Jocheved
when you use that in splash Screen , when main Activity start stack of activity is Clear, so if you hit back key application going to exit and SplashActivity not run again in your app. - Shayan Pourvatan
do I need to put finish(); in mainactivity - Jocheved
no you don't need. Please try first. - Shayan Pourvatan

2 Answers

2
votes

for your query:

But I needed to exit from application if user press back button from MainActivity.

For that you need to write this in MainActivity

@Override
    public void onBackPressed() {
        finish();
    }

Update:

put this line to exit from app:

moveTaskBack(true);

0
votes

use System.exit(0) in onBackPressed() like this:

@Override
public void onBackPressed() {
   // TODO Auto-generated method stub  
   super.onBackPressed();    

   System.exit(0);
}