source.rar
source.rar

Reputation: 8080

Why does Android recreate activities on Orientation Change

Does anyone know the rationale behind the design of Android to destroy and re-create activities on a simple orientation change?

Shouldn't allowing the activity to just redraw itself (if it so chooses) be a better, simpler and more practical design?

BTW, I'm well aware of how to disable my app from orientation change effects, but what I don't really get is the reason for this design in Android

Upvotes: 17

Views: 4948

Answers (3)

user1923613
user1923613

Reputation: 636

This is pretty simple. Android destroys and recreates an activity on orientation changes so that you (the developer) have the option to keep on using the previous layout or use and implement another one.

Upvotes: 0

sat
sat

Reputation: 41076

In docs, http://developer.android.com/guide/topics/resources/runtime-changes.html

it states that,

The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources.

Upvotes: 12

Abhishek
Abhishek

Reputation: 1779

Dont know exactly why, my guess is because it has to reconstruct the activity on the screen.

OnCreate takes in a variable "Bundle savedInstanceState". You can save the data in there for faster reloading.

package com.android.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTextView = new TextView(this);

        if (savedInstanceState == null) {
            mTextView.setText("Welcome to HelloAndroid!");
        } else {
            mTextView.setText("Welcome back.");
        }

        setContentView(mTextView);
    }

    private TextView mTextView = null;
}

Upvotes: 0

Related Questions