0

Is there an easy way to save a whole viewgroup with its data together? For example if initially i have this view and during the app run some of the view's background image changes, some views become invisible, or its' text changes, etc. How can I save the state of the view, in case user quits app?

<RelativeLayout android:layout_width="wrap_content" android:id="@+id/water_room_common"
    android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <!-- storage layout -->
<View android:id="@+id/gap" android:layout_width="wrap_content"
        android:layout_height="0dip" android:layout_alignParentBottom="true" />

    <RelativeLayout android:id="@+id/storage"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_above="@id/gap" android:layout_marginBottom="10dp"
        android:layout_marginRight="27dp" android:layout_marginLeft="10dp"
        android:layout_toLeftOf="@id/bag" android:orientation="horizontal"
        >

        <ImageButton android:id="@+id/ImageButton02"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignTop="@+id/ImageButton01"
            android:layout_marginRight="16dp" android:layout_toLeftOf="@+id/ImageButton01"
            android:background="@drawable/single_square" android:onClick="itemSelected" />

        <ImageButton android:id="@+id/ImageButton03"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignTop="@+id/ImageButton01"
            android:layout_centerHorizontal="true" android:layout_marginRight="16dp"
            android:layout_toLeftOf="@+id/ImageButton02" android:background="@drawable/single_square"
            android:onClick="itemSelected"
            />

        <ImageButton android:id="@+id/ImageButton04"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignTop="@+id/ImageButton01"
            android:layout_marginRight="16dp" android:layout_toLeftOf="@+id/ImageButton03"
            android:adjustViewBounds="true" android:background="@drawable/single_square"
            android:onClick="itemSelected"
            />

        <ImageButton android:id="@+id/ImageButton05"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignTop="@+id/ImageButton01"
            android:layout_marginRight="16dp" android:layout_toLeftOf="@+id/ImageButton04"
            android:adjustViewBounds="true" android:background="@drawable/single_square"
            android:onClick="itemSelected"
            />

        <ImageButton android:id="@+id/ImageButton06"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignTop="@+id/ImageButton01"
            android:layout_marginRight="16dp" android:layout_toLeftOf="@+id/ImageButton05"
            android:adjustViewBounds="true" android:background="@drawable/single_square"
            android:onClick="itemSelected"
             />

        <ImageButton android:id="@+id/ImageButton01"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true" android:background="@drawable/single_square"
            android:onClick="itemSelected" 
             />
    </RelativeLayout>

    <ImageButton android:id="@+id/bag" android:layout_width="65dp"
        android:layout_height="65dp" android:layout_alignBottom="@+id/storage"
        android:layout_alignParentRight="true" android:layout_marginRight="10dp"
        android:adjustViewBounds="true"
        android:onClick="bagClicked"/>
    <ImageButton android:id="@+id/left" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignLeft="@+id/storage"
        android:layout_centerVertical="true" android:background="@null"
        android:onClick="previousWall" android:src="@drawable/left" />
    <ImageButton android:id="@+id/right" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignRight="@id/bag"
        android:layout_centerVertical="true" android:background="@null"
        android:onClick="nextWall" android:src="@drawable/right" />

</RelativeLayout>
Nazerke
  • 2,098
  • 7
  • 37
  • 57

1 Answers1

0

See this answer. You need to override onSaveInstanceState for transient changes or override onPause and onResume for permanent changes.

Read the API guide here

Update

This is a summary of this training Picture this scenario. You have a notepad app. On the homescreen, the user has a list of recently created notes (NoteList Activity) and the user can choose to create a new note or edit an existing note and changes will be stored in a database. When the user clicks on an item brings another activity (NoteEditActivity) to the foreground. If the user is in the process of editting a note and the NoteEditActivity loses focus for some reason (e.g a call comes in or the user just decides to switch to another app without clicking the "confirm button") the NoteEditActivity goes from being focused to being paused and then shortly after it is stopped. To prevent the user from loosing the current edit, you need to call onPause where you would want to save any changes to the database. Now if the app is in the stopped (onStop) and the user returns to the edit it will not go through onCreate but rather through onResume which is where you want to "resume" from where the user left.

This picture should further explain. Notice how onResume gets called every time the activity is about to be focused and not onCreate.

You can follow the training above for better understanding.

enter image description here

Community
  • 1
  • 1
Jimi
  • 539
  • 5
  • 21
  • onSaveInstanceState and onRestoreInstanceState saves only when device kills the app because of lack of memory or when the orientation changes which is not problem for me cause my app is landscape only. I need to save view not only in transient changes. as for overriding onPause and onResume do you have any exact suggestions?As it was actually my question – Nazerke Nov 29 '13 at 09:06
  • I want to save the view not the data such as strings, int, etc, not user input. You know the xml that we define as layout file in setContentView(layoutA). During the app run this layoutA gets changed: some of its imagebuttons change background image, some buttons become invisible, etc. So I want to save new version of layoutA onPause() and recreate it onResume – Nazerke Nov 29 '13 at 13:15