1 / 19

Passing Data Between Activities

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.

trory
Télécharger la présentation

Passing Data Between Activities

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Passing Data Between Activities

  2. 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.

  3. 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.

  4. How to Launch a new Activity from an Activity? Intent i = new Intent(this, ActivityTwo.class); startActivity(i);

  5. How to Launch a new Activity from an Activity? Intent i = new Intent(this, ActivityTwo.class); startActivity(i); Parm 1: The Context

  6. 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.

  7. 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.

  8. Storing Data in the Intent Intent i = new Intent(this, ActivityTwo.class); i.putExtra("UserName", "John Doe"); i.putExtra("DLNumber", 123456789); startActivity(i);

  9. 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.

  10. Extracting Data from the Intent Intent 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"); intdl = i.getIntExtra("DLNumber", 0);

  11. How to get a result from an Activity

  12. startActivityForResult() • From ActivityOne, you can start ActivityTwo by callingstartActivityForResult(). Using this method, you can specify a request code. • When ActivityTwois about to end, you can call setResult() to pass data back to ActivityOne. • Once ActivityTwoends, if it called setResult(), then the onActivityResult() method on ActivityOne is executed. ActivityOnecan now access the data sent to it from ActivityTwo.

  13. You want to take a photo? Activity A Activity B Create an Intent to Launch the Camera App In 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. Contacts App

  14. You want to take a photo? Activity A Activity B //Code in Activity A Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // start the image capture IntentstartActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); Contacts App

  15. When the camera activity completes The contacts app will receive a call in onActivityResult(). Contacts App

  16. When the camera activity completes //Code in Activity B Intent 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(); Contacts App

  17. Implementing startActivityForResult() • Code in ActivityOne.java REQUEST_CODE is a user defined integer. I usually store the REQUEST_CODE as a publicstatic final member so it can be accessed in both activities.

  18. Returning result from SubActivity Code from ActivityTwo.java

  19. Implementing startActivityForResult() • Code in ActivityOne.java

More Related