1. Giulio Piancastelli
  2. android-activity-lifecycle

Commits

Giulio Piancastelli  committed 98dcb29

Exchange data from first to second activity.

  • Participants
  • Parent commits a6ed2d3
  • Branches default

Comments (0)

Files changed (3)

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

View file
         launchButton.setOnClickListener(new View.OnClickListener() {
 			public void onClick(View v) {
 				Intent intent = new Intent(A.this, B.class);
+				intent.putExtra(EXTRA_COUNTER, counter);
 				startActivity(intent);
 			}
 		});
         Button incButton = (Button) findViewById(R.id.incButton);
         incButton.setOnClickListener(new View.OnClickListener() {
 			public void onClick(View v) {
-				counter++;
+				if (counter == 25)
+					counter = 0;
+				else
+					counter++;
 				showCounterValue();
 			}
 		});
         Button decButton = (Button) findViewById(R.id.decButton);
         decButton.setOnClickListener(new View.OnClickListener() {
 			public void onClick(View v) {
-				counter--;
+				if (counter == 0)
+					counter = 25;
+				else
+					counter--;
 				showCounterValue();
 			}
 		});

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

View file
 package it.synergia.courses.android.lifecycle;
 
+import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 				showLetterValue();
 			}
 		});
+		// B is designed to be a sub-activity called from another activity
+		// (A in this case), to it's always created starting from an Intent
+		Intent intent = getIntent();
+		int value = intent.getIntExtra(EXTRA_COUNTER, 0);
+		letter += value;
 		letterView = (TextView) findViewById(R.id.letter_value);
 		showLetterValue();
 		log("ON_CREATE");

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

View file
 public class LifecycleActivity extends Activity {
 
 	private static final String TAG = "ANDROID_LIFECYCLE";
+	
+	static final String EXTRA_COUNTER =
+			"it.synergia.courses.android.lifecycle.COUNTER_VALUE";
 
 	protected void log(String method) {
 		Log.i(TAG, method + " " + getClass().getSimpleName());