Evitar que la función onDraw() se reinicie durante los cambios de configuración

I have an activity which basically sleeps for a time and initialize another activity. During this sleep time I make some animated drawings on screen.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    View splashScreen = new SplashScreenView(this);
    setContentView(splashScreen);
    Thread splashThread = new Thread(){
        public void run(){
            try{
                sleep(6500);
                startActivity(new Intent("com.example.LOGIN"));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{
                finish();
            }
        }
    };
    splashThread.start();
}

But when the screen orientation changes, the activity was restarting. So I handled it by modifying manifest file.

<activity
        android:name=".SplashScreenActivity"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

I have one problem left. I prevent activity from restarting but the onDraw() function in my SplashScreenView class still restarts.

protected void onDraw(Canvas canvas) {

    canvas.drawBitmap(dstbmp2, logoX, logoY, null);
    canvas.drawBitmap(dstbmp, ballX, ballY, null);

    // Update the position of the ball.

    update();

    // Delay
    try {  
        Thread.sleep(100);  
    } catch (InterruptedException e) { }

    invalidate();

}

preguntado el 04 de julio de 12 a las 09:07

1 Respuestas

Override this method in your Activity:

public void onConfigurationChanged(Configuration newConfig) {

    }        

Declare a global boolean variable and set it in this method for example true,then in onCreate() you can decide do you really would to create new instance of splashScreen y empezar splashThread o no.

Respondido 04 Jul 12, 10:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.