4

I am building my first app and am almost done. (yay!) Now all I have to do is to set up the Java for the onConfigurationChange used when the orientation is changed as well as the keyboard is pulled out. First off, the problem is that when the orientation changes everything gets erased because a new new layout is put in place by the setContentView method. This of course makes sense, however I was wondering if there was any way or possible workaround that I can have the edittext and textview values stay constant when the orientation/keyboard is changed. I have tried various things, such as taking the string values before the setContentView, however I found that this would only result in a NullPointerException.

Basically I'm trying to keep the edittext and textview values the samne as the orientation or whatever changes.

Here is a brief summary of my code for a reference

This is NOT the actual code, just a summary pf what I'm doing.

    public class MainActivity extends Activity {

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

     //do some stuff that is the core of the app. Nothing that would impact this question though

    }

      public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);

     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){

        setContentView(R.layout.landscape);
            // same code that was within the onCreate. Just doing stuff related to  my  app 

    }if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

        setContentView(R.layout.main);
            // same code that was within the onCreate. Just doing stuff related to  my  app
    }
         }
    ' }

And yes, I did write in my AndroidManifest file the android:configChanges="orientation|keyboardHidden" The app works like it should, I just don't have the text viwes and edittexts being updated. Thanks!

Dan
  • 61
  • 2
  • 4

2 Answers2

1

however I was wondering if there was any way or possible workaround that I can have the edittext and textview values stay constant when the orientation/keyboard is changed.

Doing it this way...no. When you call setContentView() it puts everything to default so TextViews will have its default text, etc... You could, as you said, try to save the Strings and set them again but this would get messy. You could handle it the "Android recommended" way and use Handling Runtime Changes. However, I currently handle all of them myself but I don't call setContentView() during orientation changes.

If you were to tell us why you were doing it this way then we may be able to give you a better solution. But I wouldn't suggest calling setContentView() multiple times like this

Small Example

@Override
    public void onConfigurationChanged(Configuration newConfig) {
      newConfig = Globals.getUserLanguage(this);
      super.onConfigurationChanged(newConfig);
      if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
      {
          btnSave = (Button) findViewById(R.id.lsSaveBtn);
          Button btnHide = (Button) findViewById(R.id.button1);
          btnHide.setVisibility(Button.GONE);
          btnSave.setVisibility(Button.VISIBLE);
    //    btnSave.setOnClickListener();
      }
      else
      {
          btnSave = (Button) findViewById(R.id.button1);    
          Button btnHide = (Button) findViewById(R.id.lsSaveBtn);
          btnHide.setVisibility(Button.GONE);
          btnSave.setVisibility(Button.VISIBLE);
      }
    }
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I was doing it this way as my programming experience is low and the only way I knew of switching the way things looked was to use a new layout. I also thought it was easier but I guess I was wrong lol. This was before I knew of some tactics to use in the java to change the UI. Anyways is there any kind of method that I can use that would listen for changes in the edittext/textview and I use that info to update the new ones? – Dan Jun 14 '13 at 02:49
  • I understand being new...don't worry, you will get it. I'm not sure exactly what you are asking but I have added a small example of an `Activity` where I show or hide certain `View`s depending on `orientation` – codeMagic Jun 14 '13 at 03:10
0

You can save the value in savedInstanceState.

As your activity begins to stop, the system calls onSaveInstanceState() so your activity can save state.

As to restore the value, you can use

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

or

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}

You can also put your layout in layout folder for potrait and layout-land for landscape so you don't need to call setContentView() in the onConfigurationChanged() everytime the apps restart the activity

link : activity recreating

Sieryuu
  • 1,510
  • 2
  • 16
  • 41