0

I cant seem to be able to modify the view of my layouts after the first initialization of the android app in onCreate(null), when I minimize the app and open it up again onCreate(savedInstanceState), all my functions like setText don't seem to matter

enter image description here

package com.example.www.i_fucking_hate_java;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null)
        {
            // this will work
            ((EditText) findViewById(R.id.editText)).setText("Good afternoon");
            Log.d("Me", "Running 1 " + ((EditText) findViewById(R.id.editText)).getText().toString());
        }
        else
        {
            // k so we have an old instance,
            // problem is now our "handles" to our EditTexts are useless

            // THIS WILL NOT WORK ??????????????????????????????????????????
            // the value returned by getText is correct, but the emulator display
            // does not change - also I hate android
            ((EditText) findViewById(R.id.editText)).setText("Good night");
            Log.d("Me", "Running 2 " + ((EditText) findViewById(R.id.editText)).getText().toString());
        }

//        // this will work
//        ((EditText) findViewById(R.id.editText)).setText("Good morning");
//        Log.d("Me", "Running 3 "  + ((EditText) findViewById(R.id.editText)).getText().toString());
    }
}

Why is my app stuck on "Good afternoon" and not displaying "Good night" as according to the logger?

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • Try use in following manner stack[http://stackoverflow.com/questions/16769654/how-to-use-onsaveinstancestate-and-onrestoreinstancestate] – Arjun May 08 '17 at 05:00

1 Answers1

2

In default view state will save and restore in onRestoreInstanceState. as onRestoreInstanceState call after onCreate even you change your text in onCreate it's will change back to save state in onRestoreInstanceState.

Your solution is move your view restoration from onCreate to onRestoreInstanceState

When onRestoreInstanceState will call?

This method is called between onStart() and onPostCreate(Bundle).

As your activity begins to stop, the system calls the onSaveInstanceState() method so your activity can save state information with a collection of key-value pairs. The default implementation of this method saves transient information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView widget. Your app should implement the onSaveInstanceState() callback after the onPause() method, and before onStop(). Do not implement this callback in onPause().

For more info see Documentation

Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • Where is the documentation that says "In default view state will save and restore in onRestoreInstanceState" – AlanSTACK May 08 '17 at 05:16
  • It's related to lifeCycle ( in your case activity lifeCycle) in case like rotation changed that activity must to destroy and recreate, all value will save in onSaveInstance and restore in onRestoreInstance, you can change this behave from manifest file. – Shayan Pourvatan May 08 '17 at 05:19
  • thank you so much it finally worked - literally would never have solved it without your help (spent 6+ hours on this) – AlanSTACK May 08 '17 at 05:39
  • I'm glad to help you – Shayan Pourvatan May 08 '17 at 05:44