Search code examples
androidout-of-memorysplash-screendynamic-splash-screen

Activity start again over the old activity when rotate


I have two Splash screen first is static and after him start second splash screen who switch diffrent splash screens randomly.My problem is when start second activity and till activity works rotate the phone second activity start again over the old one.I worry becouse when splash screen over and my app start i got two working app.

This is my first static splash screen.

package com.readytechgo;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Intent;

public class FirstSplashScreen extends DefaultActivity {

    int timeout = 2000; // Choose the delay (1000 = 1 second)

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

        setFonts();
         // Randomise a background
        int[] yourListOfImages= {R.drawable.intro};

        Random random = new Random(System.currentTimeMillis());
        int posOfImage = random.nextInt(yourListOfImages.length);

        ImageView imageView= (ImageView) findViewById(R.id.imageView1);
        imageView.setBackgroundResource(yourListOfImages[posOfImage]);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                //Redirecting to the home page
                Intent redirect = new Intent(getApplicationContext(), SplashScreen.class);
                startActivity(redirect);
                finish();
            }
        }, timeout);
    }
}

And this is my second splash screen... with him i have problems....

 package com.readytechgo;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Intent;

public class SplashScreen extends DefaultActivity {

    int timeout = 2000; // Choose the delay (1000 = 1 second)

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.random_splash);

        setFonts();
         // Randomise a background
        int[] yourListOfImages= {R.drawable.home_image1_portrait,R.drawable.home_image2_portrait,R.drawable.home_image3_portrait,R.drawable.home_image4_portrait};

        Random random = new Random(System.currentTimeMillis());
        int posOfImage = random.nextInt(yourListOfImages.length);

        ImageView imageView= (ImageView) findViewById(R.id.randomImageView);
        imageView.setBackgroundResource(yourListOfImages[posOfImage]);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                //Redirecting to the home page
                Intent redirect = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(redirect);
                finish();
            }
        }, timeout);
    }
}

Solution

  • You can add manage the configuration changes on the manifest xml .

    <activity
            android:name="youractivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name" >
        </activity>