-3

Initially, the first version of my app worked fine. Later, after changing to the sidebar drawer application template, it does not function properly, crashing the application (due to the splash screen). What could be the issue?

This is the code for the SpashScreen.java file:

public class SplashScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    Thread myThread = new Thread(){
        @Override
        public void run(){
            try {
                sleep(3000);
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    myThread.start();
}

}

This is the manifest:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/example"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SplashScreen"
        android:label="@string/example"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity"></activity>
</application>
Jack D.
  • 31
  • 5

5 Answers5

1

try this

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 your app main activity
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, 3000);// time in milliseconds
AskNilesh
  • 63,753
  • 16
  • 113
  • 150
  • Thanks, where exactly should I write this code? I pasted only this segment in my SplashScree.java class and it had a lot of errors – Jack D. Jul 21 '17 at 14:13
  • Use finish() isn't well solution because in the future you may be need to add animation, (This solution will create problem), Use Intent flag for close prevues activity. – Vova Jul 21 '17 at 14:14
0

Try this might help

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, 3000);
akhilesh0707
  • 6,135
  • 4
  • 41
  • 48
0

Try to use :

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashScreen.this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }, 3000);

This code don't create new Thread, P.S. Android cannot start activity from another Thread (only form UI)

Vova
  • 817
  • 7
  • 21
0

startActivity() need to be run in main thread. Using postDelay() of Hanlder class

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }, 3000);
nhp
  • 3,642
  • 1
  • 14
  • 27
  • so I should put this fragment of code in my MainActivity? – Jack D. Jul 21 '17 at 14:18
  • In your onCreate method. Read this for understanding how to work with process and thead:https://developer.android.com/guide/components/processes-and-threads.html?hl=vi – nhp Jul 21 '17 at 14:19
  • Thank you very much for the link. I put the code in the onCreate method but now it crashes before even showing the splash screen, thank you anyway – Jack D. Jul 21 '17 at 14:23
  • I don't know. I am going to study and understand better how threads work so I can find a soloution. The weird thing for me is that the code I initially wrote worked just fine for the empty activity project, but doesn't work for the drawer side bar project, which has the same code basically... – Jack D. Jul 21 '17 at 14:33
  • Both are activities. Not make sense – nhp Jul 21 '17 at 14:34
0

using handler can solve this do below code it will work for you

  handler.postDelayed(
            new Runnable() {
                public void run() {
                    Intent intent = new Intent(youractivity.this, MainActivity.class);
            startActivity(intent);
                }
            }, 3000);
Deep Naik
  • 473
  • 1
  • 3
  • 10