Commits

Giulio Piancastelli  committed 32b3c2f

Add state and save/restore machinery to second activity.

  • Participants
  • Parent commits a3761d3
  • Tags lifecycle_with_state

Comments (0)

Files changed (3)

File res/layout/b.xml

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" android:text="@string/b_label"/>
+    <TextView android:layout_height="wrap_content" android:text="@string/letter"
+		android:layout_margin="5dp" android:layout_width="match_parent" android:id="@+id/letter_value"/>
+	<LinearLayout android:layout_width="match_parent"
+		android:layout_height="wrap_content" android:id="@+id/linearLayout2">
+
+		<Button android:text="Next" android:layout_height="wrap_content"
+			android:id="@+id/nextButton" android:layout_weight="0.5"
+			android:layout_width="fill_parent" />
+		<Button android:layout_width="fill_parent" android:id="@+id/prevButton"
+			android:layout_height="wrap_content" android:text="Previous"
+			android:layout_weight="0.5"></Button>
+	</LinearLayout>
 </LinearLayout>

File res/values/strings.xml

     <string name="b_label">Activity B</string>
     <string name="launch_b_button">Launch B</string>
     <string name="counter">Counter: </string>
+    <string name="letter">Letter: </string>
 </resources>

File src/it/synergia/courses/android/lifecycle/B.java

 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
 
 public class B extends Activity {
 	private static final String TAG = "ANDROID_LIFECYCLE";
+	
+	private char letter = 'a';
+	private TextView letterView;
 
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
 		setContentView(R.layout.b);
+		Button nextButton = (Button) findViewById(R.id.nextButton);
+		nextButton.setOnClickListener(new View.OnClickListener() {
+			public void onClick(View v) {
+				if (letter == 'z')
+					letter = 'a';
+				else
+					letter += 1;
+				showLetterValue();
+			}
+		});
+		Button prevButton = (Button) findViewById(R.id.prevButton);
+		prevButton.setOnClickListener(new View.OnClickListener() {
+			public void onClick(View v) {
+				if (letter == 'a')
+					letter = 'z';
+				else
+					letter -= 1;
+				showLetterValue();
+			}
+		});
+		letterView = (TextView) findViewById(R.id.letter_value);
+		showLetterValue();
 		log("ON_CREATE");
 	}
 
 	private void log(String method) {
 		Log.i(TAG, method + " " + getClass().getSimpleName());
 	}
+	
+	private void showLetterValue() {
+		String letterLabel = getResources().getString(R.string.letter);
+		letterView.setText(letterLabel + String.valueOf(letter));
+	}
+
+	@Override
+	protected void onRestoreInstanceState(Bundle savedInstanceState) {
+		super.onRestoreInstanceState(savedInstanceState);
+		letter = savedInstanceState.getChar("letter");
+		showLetterValue();
+		log("ON_RESTORE_INSTANCE_STATE");
+	}
+
+	@Override
+	protected void onSaveInstanceState(Bundle outState) {
+		super.onSaveInstanceState(outState);
+		outState.putChar("letter", letter);
+		log("ON_SAVE_INSTANCE_STATE");
+	}
 }