0

I am working on an application in which I want to exit the app onBackPressed. I have wrote code for it but the problem is this when I press back button my app exits and not cleared from RAM and when I reopen the app after exit it opens from second screen not from splash. What is the problem?

Code:

moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
finish();
System.exit(0);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
D Developer
  • 73
  • 1
  • 9

2 Answers2

0

Best way of handle splash screen is clear splash stack from Intent. So you don't need to kill your application manually

Intent intent = new Intent(splashActivity.this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent);
finish();

Don't forgot to remove below code

moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
finish();
System.exit(0);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
PriyankVadariya
  • 809
  • 9
  • 14
0

Put this in onCreate() splash screen activity .

 @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.activity_splash_screen);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                            intent = new Intent(ActivitySplachScreen.this, MainActivity.class);  
                            intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            finish();
                }
            }, 3000);
        }

& activity_splash_screen is.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash"
    android:orientation="vertical">
</LinearLayout>