SplashScreen doesn't show main activity but it automatically closes the app after showing the splashscreen

Viewed 203
<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="App Name"
    android:roundIcon="@drawable/logo"
    android:supportsRtl="true"
    android:requestLegacyExternalStorage="true"
    android:theme="@style/maintheme"
    android:usesCleartextTraffic="true"
    tools:ignore="GoogleAppIndexingWarning"
    tools:replace="android:icon, android:allowBackup"

    >

this activity is splash screen activity below

    <activity
        android:name=""
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

this activity is Folder activity activity below

    <activity
         android:name= "" 
         android:theme= ""
         android:configChanges="orientation|keyboardHidden|screenSize"
         android:exported="true" >
           <intent-filter>
              <action android:name="android.intent.action.MAIN" />

              <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>



    <activity android:name=".MainActivity" 
   android:configChanges="orientation|keyboardHidden|screenSize"
        android:exported="true">
    </activity>

    <activity android:name="" android:theme=""/>



    <activity android:name="" android:theme=""  android:configChanges="orientation|keyboardHidden|screenSize"/>

</application>

the problem is when the app run it show the splash screen and then it show the app keep closing,it doesn't start the main activity. can i make the android manifest to make the splash screen show first and then the start main activity

4 Answers

You cannot use your manifest for this, you need to use an Intent.

Code Example that you need in your SplashScreen Activity:

Intent intent = new Intent(SplashScreen.this, YOURNEXTCLASS.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();

You can switch activities only from the java files.

Implement this in your OnCreate method.

SplashActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = new Intent(this, MainActivity.class);
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            startActivity(intent);
            finish();
        }
    }, 2000); ////wait 2s before doing the action
}

AndroidManifest.xml

<activity
        android:name=".ui.activities.SplashActivity"
        android:exported="true"
        android:theme="@style/SplashScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
<activity
        android:name=".ui.activities.MainActivity"
        android:exported="true"/>

the simple method to do is use this in splashscreenactivity.java

Intent intent=new Intent(SplashScreen.this, YourClass.class);
               startActivity(intent);
               finish();

like this

public class SplashScreenActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_splash_screen);
       new Handler().postDelayed(new Runnable() {
           @Override
           public void run() {
            //This method will be executed once the timer is over
            // Start your app main activity
           Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
           startActivity(i);
            // close this activity
           finish();
           }
        }, 3000);
    }
}
Related