aalionline
aalionline

Reputation: 448

Save Data of previous layout on orientation change while implementing onConfigChange()

If the user is putting data in edittext fields and orientation get changed, the onConfigChange method is called and new layout is populated,with empty edittext fields, i want to retain the data in edittext fields entered by user befor orientation change. Any kind of help will highly appreciated.

 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            int orientation_default = getResources().getConfiguration().orientation;
            switch (orientation_default) {
            case Configuration.ORIENTATION_PORTRAIT: {
                setContentView(R.layout.registration);

                break;
            }
            case Configuration.ORIENTATION_LANDSCAPE: {
                setContentView(R.layout.registration_horizontal);
                break;
            }

            }
            findViewById(R.id.backtohomepage).setOnClickListener(this);
            findViewById(R.id.backtologinpage).setOnClickListener(this);
            findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
            findViewById(R.id.termsandconditions_id).setOnClickListener(this);
            findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {

            super.onConfigurationChanged(newConfig);
            if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                setContentView(R.layout.registration);
                findViewById(R.id.termsandconditions_id).setOnClickListener(this);
                findViewById(R.id.backtohomepage).setOnClickListener(this);
                findViewById(R.id.backtologinpage).setOnClickListener(this);
            } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                setContentView(R.layout.registration_horizontal);
            }

            findViewById(R.id.backtologinpage).setOnClickListener(this);
            findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
            findViewById(R.id.termsandconditions_id).setOnClickListener(this);
            findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
        };

Upvotes: 0

Views: 2933

Answers (2)

NikhilReddy
NikhilReddy

Reputation: 6954

<activity
            android:name="com.Dashboard"
             android:configChanges="keyboardHidden|orientation"
            >
        </activity>

paste this in your manifest... http://developer.android.com/guide/topics/resources/runtime-changes.html .. go through this link..

Upvotes: 1

Dante
Dante

Reputation: 1094

Put android:configChanges="orientation" for that activity node in the manifest.

EDIT: Check this question : Activity restart on rotation Android

Upvotes: 1

Related Questions