ayansinha
ayansinha

Reputation: 85

I am having issue with my splash screen as am not getting into the next activity

This is an example of splash screen but facing difficulty with it as I couldn't view my MainActivity class, couldn't be able to recognize the issue.Tried in manifest file by changing the action and the category name as well but could not be able to resolve to it.

Basically changed my Intent intent = new Intent(); as well but still the same goes on.

    public class SplashActivity extends Activity
    {

        MediaPlayer player;

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);

            player = MediaPlayer.create(SplashActivity.this, R.raw.splash);
            player.start();
            Thread timer = new Thread() 
            {

                @Override
                public void run() 
                {
                    try
                    {               
                        sleep(4000);

                    }catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }

                        finish();
                        Intent intent = new Intent();
                        intent.setClass(SplashActivity.this, MainActivity.class);
                        startActivity(intent);
                        stop();




                }
            };
            timer.start();

        }

        @Override
        protected void onPause() 
        {
            super.onPause();
            player.release();
            finish();
        }


    }

====>And here is my manifest file --

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mainsplashcreen"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/android_splash"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name=".SplashActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

</manifest>

Upvotes: 0

Views: 397

Answers (3)

Paramvir Singh
Paramvir Singh

Reputation: 2404

There are a few things which you should take care of: 1. The usage of stop() and the place it is being used are not encouraged. Besides being deprecated, it will also give you UnsupportedOperationExcecption. 2. Is your MediaPlayer being initialized correctly without any errors? Check the logs. 3. What is the reason you are using finish() in onPause() method? It is not recommended. 4. You are assigning MAIN action to both your activities. While it is allowed, it should be for a specific reason.

However, all these things should not avoid your application to go to the main activity. There may be the reasons with your main activity code.

Upvotes: 0

Haresh Chaudhary
Haresh Chaudhary

Reputation: 4400

Another Very Short,Simple and Effective way of Implementing Flash Screen is as below:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Handler handler=new Handler();
        Runnable gotoMain=new Runnable() {
        public void run() 
        {
          startActivity(new Intent(SplashActivity.this, MainActivity.class));
          finish();
        }
      };
      handler.postDelayed(gotoMain, 4000);
    }  

You just Replace you OnCreate Method with this one.

Upvotes: 0

Tim Roes
Tim Roes

Reputation: 1414

I would guess, that your problem is, that you finish your activity before you start the new one. If you call finish() on your activity, it will be destroyed, and so the startActivity() won't be called anymore (or if it will be called, it won't get a valid context anymore). So try to move the finish() method, at the end of your run method, that should solve the problem.

Upvotes: 1

Related Questions