passing data between activities
DESCRIPTION
Passing Data Between Activities. Android Activities. If you remember, Android stores Activities on a stack. You have the ability to add as many Activities onto the stack from you application and needed. You can also pass data to and from Activities. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/1.jpg)
Passing Data Between Activities
![Page 2: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/2.jpg)
Android Activities
• If you remember, Android stores Activities on a stack.
• You have the ability to add as many Activities onto the stack from you application and needed.
• You can also pass data to and from Activities.
![Page 3: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/3.jpg)
How to Launch a new Activity from an Activity?
• The Activity class provides a method called startActivity()
• startActivity() takes an Intent as an argument.
• The Intent provides the information with what new Activity we want to create.
![Page 4: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/4.jpg)
How to Launch a new Activity from an Activity?
Intent i = new Intent(this, ActivityTwo.class);startActivity(i);
![Page 5: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/5.jpg)
How to Launch a new Activity from an Activity?
Intent i = new Intent(this, ActivityTwo.class);startActivity(i);
Parm 1: The Context
![Page 6: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/6.jpg)
How to Launch a new Activity from an Activity?
Intent i = new Intent(this, ActivityTwo.class);startActivity(i);
Parm 2: The Class name of the new Activity to create.
![Page 7: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/7.jpg)
Intents
• Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system.
• In addition to giving information about what Activity we want to launch, Intents can also store data to be passed between Activities.
![Page 8: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/8.jpg)
Storing Data in the IntentIntent i = new Intent(this, ActivityTwo.class);i.putExtra("UserName", "John Doe");i.putExtra("DLNumber", 123456789);startActivity(i);
![Page 9: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/9.jpg)
Extracting data stored in Intent from the new Activity
• The Activity class has a getIntent()
• Use the Intent returned from the getIntent() to extract any data that was passed.
![Page 10: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/10.jpg)
Extracting Data from the IntentIntent i = new Intent(this, ActivityTwo.class);i.putExtra("UserName", "John Doe");i.putExtra("DLNumber", 123456789);startActivity(i);
Intent i = getIntent();String name = i.getStringExtra("UserName");int dl = i.getIntExtra("DLNumber", 0);
![Page 11: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/11.jpg)
How to get a result from an Activity
![Page 12: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/12.jpg)
startActivityForResult()• From ActivityOne, you can start ActivityTwo by
calling startActivityForResult(). Using this method, you can specify a request code.
• When ActivityTwo is about to end, you can call setResult() to pass data back to ActivityOne.
• Once ActivityTwo ends, if it called setResult(), then the onActivityResult() method on ActivityOne is executed. ActivityOne can now access the data sent to it from ActivityTwo.
![Page 13: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/13.jpg)
Contacts App
Create an Intent to Launch the Camera AppIn order to get a picture for a contact.
When you start the activity, you’ll send a request code that will be returned to you when the launched activity completes.
You want to take a photo?
Activity BActivity A
![Page 14: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/14.jpg)
Contacts App//Code in Activity AIntent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// start the image capture IntentstartActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
You want to take a photo?
Activity B
Activity A
![Page 15: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/15.jpg)
Contacts App
The contacts app will receive a call in onActivityResult().
When the camera activity completes
![Page 16: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/16.jpg)
Contacts App
When the camera activity completes
//Code in Activity BIntent intent = new Intent();intent.putExtra();// It will put an image thumbnail into the intent’s data
//set results so Activity A gets onActivityResult() calledsetResult(Activity.RESULT_OK, intent);finish();
![Page 17: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/17.jpg)
Implementing startActivityForResult()
• Code in ActivityOne.java
REQUEST_CODE is a user defined integer. I usually store the REQUEST_CODE as a public static final member so it can be accessed in both activities.
![Page 18: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/18.jpg)
Returning result from SubActivity
Code from ActivityTwo.java
![Page 19: Passing Data Between Activities](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681642f550346895dd5f87b/html5/thumbnails/19.jpg)
Implementing startActivityForResult()
• Code in ActivityOne.java