I.putExtra("name", edtName.getText().toString() In the caller ( EditActivity) we can pass data in this way: Intent i = new Intent(EditActivity.this, ViewActivity.class) Reading the API Doc we know that we can add string, longs, CharSequence, int and so on.It behaves like a Map where there’s the key and the value. Intent has several method called putExtra(String name, ….) that allows us to save inside the Intent our information. Let’s suppose, moreover, that we want to pass the name, surname and the email. We have to pass this data between our activities. To analyse these methods let’s suppose we have two activities: one that allows user to enter data ( EditActivity) and the other one that shows to the user the data ( ViewActivity). Parcelable: It is a way of “serializing” our object.Bundle: create a bundle and set the data here.Direct: put our data into intents directly.Intent in android is an action description.For more information look here. This method is the most elegant one because it uses all the android features. In this case we create several method to hold the data. Otherwise, we can use the singleton pattern that ensures we have only one instance of this class in all our JVM. In this case we can create several static method to hold the data we want to share. Exploiting the Android feature that an app runs in a single process we can use a static class to exchange data between activities. This is the easiest way to achieve our goal.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |