0

Problem is that, when screen changes orientation, textview background color is gone. here is TextView definition:

<TextView
    android:id="@+id/nomResult"
    android:layout_width="fill_parent"
    android:layout_height="80sp"
    android:text="Nom result"
    android:layout_below="@id/svitrkods"
    android:layout_alignRight="@+id/svitrkods" 
    android:layout_above="@+id/exit_button"
    android:freezesText="true"
    />

App is inventory app. When product is already in inventory, then i am displaying text in textview with red background. vaiIrInvent.setBackgroundColor(0xfff00000); that works. but when screen rotates, background color is gone... When application stars, i save original background color of textview defBackColor = vaiIrInvent.getBackground(); then in app i am switching to red or default.

if (isInInventory)
{
    vaiIrInvent.setBackgroundColor(0xfff00000);
}
else
{
    vaiIrInvent.setBackgroundDrawable(defBackColor);
}

Maybe there is some option like android:freezesText ? Thanks.

Guntis
  • 496
  • 1
  • 5
  • 19

3 Answers3

1

yup rotion calls onCreate() you need to save information to restore preferences

this link should explain it a little better

Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
DJPlayer
  • 3,244
  • 2
  • 33
  • 40
  • I this already found, now i am looking how to save Drawable object I am thinking that my problem is little bit bigger. because i have 2 views. one in layout folder and one in layout-land. when screen rotates i need figure out if background is set and then pass background to landscape layout. Or its not good idea? – Guntis Oct 06 '11 at 18:14
  • problem is much bigger than i think. when screen rotates, all variables are gone. I need trigger action to edittext to re-query database. tomorrow i will continue to search some solutions. variables data i can save in `onSaveInstanceState` . Of course i can lock app in horizontal or in vertical, but i don't like that solution.. – Guntis Oct 06 '11 at 18:55
0

It looks like you lose isInInventory state on rotation because by default OS creates a new Activity instance in this case. If this is the case, then the best way is to use save/restore state callbacks the Activity class exposes. Check this for details: Saving activity state.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
0

Answer to my situation is: save scanned barcode when screen orientation changes

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  // Save entered code 
  savedInstanceState.putString("svkKods", svkKods.getText().toString());
  super.onSaveInstanceState(savedInstanceState);
}

and restore code and query db.

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  String mystr = savedInstanceState.getString("svkKods");
  svkKods.setText(mystr);
  //if code presents 
  if (mystr.length() > 3) FindKods(mystr); //query database and fill all texts and variables
}

i am doing this way because in a variable is stored state, when barcode is found in database and then user can submit changes. otherwise is displayed message to must find barcode ...

Guntis
  • 496
  • 1
  • 5
  • 19