sai
sai

Reputation: 558

Android + Splash Screen crashes total on orientation change

I actually have 3 activities in my app.

I have just created an activity and Made it as a SPLASH SCREEN using handler.

i.e., My splash screen appears for 3 seconds and then the main lifecycle of the app continues. upto that its all perfect.

My Problem is When the splash screen is loading, if I change orientation, the total app crashes.

My requirement is to load app in both landscape and portrait modes.

I have tried onConfig changes etc., but in vain....

My sad story contains all here....

    public class Asplash extends Activity{
Handler handler = new Handler();

   @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    try {

        handler.postDelayed(new Runnable() {

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


                finish();
                Intent i = new Intent(Asplash.this, Example.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(i);
            }
        }, 3000);

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

}




  @Override
protected void onPause() {
    // TODO Auto-generated method stub
     handler.removeCallbacksAndMessages(null);
       finish();
    super.onPause();

}
}

Here is Manifest file:

     <activity android:name=".Asplash"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
        android:configChanges="orientation">
        <intent-filter >
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>


    <activity
        android:name="com.example.Example"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

I just want to make this "Asplash" Activity, to appear in both landscape and portrait orientations. I also tried creating XML files for "splash"in both LAYOUT & LAYOUT-LAND Folders.Then also same Panic...

Actually In ANDROID, It should automatically adjust for ORIENTATION changes as like in the basic examples.But I can't understand why it is not working here...

Upvotes: 0

Views: 1489

Answers (5)

Vishal Vijay
Vishal Vijay

Reputation: 2528

Use this code :

public class Asplash extends Activity{

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        startLoading();
    }
    //Start new activity and finish splash activity
    public void openMainActivity() {
        Intent i = new Intent(Asplash.this, Example.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
         startActivity(i);
         finish();
    }
    //Start thread which will wait for 3 secs and call openMainActivity
    private void startLoading(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                }
                openMainActivity();
            }
        }).start();
    }
}

finish() should call after opening the new activity. And try this code it will solve your problem. You no need to make any changes to manifest.

Upvotes: 4

Aashish Bhatnagar
Aashish Bhatnagar

Reputation: 2605

Try to place below one in your manifest for the activity which u got error:

   android:configChanges="orientation"

to the activity tag and then override onConfigurationChanged method and do something like this 

    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
        {

                  load your xml file for landscape
}


else
{
 load your xml file for potrait
}

Upvotes: 0

class stacker
class stacker

Reputation: 5347

I'm not exactly sure in this case, but if the code you show is complete, then your Runnable's context is gone by the time it is run, because your Activity has been destroyed and re-created in the meantime due to the orientation change.

Activity created -> creates Runnable with itself as context -> orientation change -> Activity gets destroyed -> Activity gets created -> Runnable is being executed -> Context object of the runnable is invalid

To fix this in this very simple case, I think you could use the Application object as the Context for your RUnnable.

However, please note that you have another problem, namely that the re-creation of your Activity starts another Runnable, which will do the same, only later.

A simplistic solution for this low complexity scenario could be to store a reference to the Runnable in your derived Application object and check for its existence.

Upvotes: 0

AndroidEnthusiastic
AndroidEnthusiastic

Reputation: 931

Try to add in android manifest file

    android:configChanges="orientation|screenSize".

However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

Upvotes: 0

Avadhani Y
Avadhani Y

Reputation: 7636

Try to place below one in your manifest for the activity which u got error:

   android:configChanges="orientation"

As the Android Documentation says:

android:configChanges:

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

Upvotes: 0

Related Questions