Commits

Giulio Piancastelli  committed c27290b

Start activity for result, and finish it.

  • Participants
  • Parent commits 98dcb29
  • Tags lifecycle_with_data_exchange

Comments (0)

Files changed (4)

File res/layout/b.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="vertical"
-    android:layout_width="fill_parent"
-    android:layout_height="fill_parent"
-    >
-<TextView  
-    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"/>
+	android:orientation="vertical" android:layout_width="fill_parent"
+	android:layout_height="fill_parent">
+	<LinearLayout android:layout_width="fill_parent"
+		android:layout_height="fill_parent" android:layout_weight="1"
+		android:orientation="vertical">
+		<TextView 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="1"
+				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="1" />
+		</LinearLayout>
+	</LinearLayout>
 	<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_height="wrap_content">
+		<Button android:text="OK" android:layout_height="wrap_content"
+			android:id="@+id/okButton" android:layout_weight="1"
 			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>
+		<Button android:layout_width="fill_parent" android:id="@+id/cancelButton"
+			android:layout_height="wrap_content" android:text="Cancel"
+			android:layout_weight="1" />
 	</LinearLayout>
 </LinearLayout>

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

 package it.synergia.courses.android.lifecycle;
 
+import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 	private int counter = 0;
 	private TextView counterView;
 	
+	private static final int B_ACTIVITY = 1;
+	
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
 			public void onClick(View v) {
 				Intent intent = new Intent(A.this, B.class);
 				intent.putExtra(EXTRA_COUNTER, counter);
-				startActivity(intent);
+				startActivityForResult(intent, B_ACTIVITY);
 			}
 		});
         Button incButton = (Button) findViewById(R.id.incButton);
 		outState.putInt("counter", counter);
 		log("ON_SAVE_INSTANCE_STATE");
 	}
+
+	@Override
+	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+		super.onActivityResult(requestCode, resultCode, data);
+		switch (requestCode) {
+		case B_ACTIVITY:
+			if (resultCode == Activity.RESULT_OK) {
+				char letter = data.getCharExtra(EXTRA_LETTER, 'a');
+				counter = letter - 'a';
+				showCounterValue();
+			}
+			break;
+		}
+	}
 }

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

 package it.synergia.courses.android.lifecycle;
 
+import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 		Intent intent = getIntent();
 		int value = intent.getIntExtra(EXTRA_COUNTER, 0);
 		letter += value;
+		// Set up the OK and Cancel buttons to return the proper result to
+		// the calling activity
+		Button okButton = (Button) findViewById(R.id.okButton);
+		okButton.setOnClickListener(new View.OnClickListener() {
+			public void onClick(View v) {
+				Intent data = new Intent();
+				data.putExtra(EXTRA_LETTER, letter);
+				setResult(Activity.RESULT_OK, data);
+				finish();
+			}
+		});
+		Button cancelButton = (Button) findViewById(R.id.cancelButton);
+		cancelButton.setOnClickListener(new View.OnClickListener() {
+			public void onClick(View v) {
+				setResult(Activity.RESULT_CANCELED);
+				finish();
+			}
+		});
+		// Display the actual letter on the activity screen
 		letterView = (TextView) findViewById(R.id.letter_value);
 		showLetterValue();
 		log("ON_CREATE");

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

 	
 	static final String EXTRA_COUNTER =
 			"it.synergia.courses.android.lifecycle.COUNTER_VALUE";
+	static final String EXTRA_LETTER =
+			"it.synergia.courses.android.lifecycle.LETTER_VALUE";
 
 	protected void log(String method) {
 		Log.i(TAG, method + " " + getClass().getSimpleName());