Friday, July 10, 2015

Passing Data Between Activity in Android (Forward)

Last post about send/pass data to server...

This time...about passing data between activity, hmmm....maybe little bit related to storage..
but still focus on passing the data...
For this post, I only explain how to send to next activity. Send backward, I will explain on next post.

The technique/method I will use in this post is,
  1. Static variable/class
  2. Singleton
  3. SharedPreferences
  4. Intent
  5. Application


let's move to the first technique,



Static Variable


Static variable is a one of basic knowledge you must know if you are "Javatar".
However, there are issue regarding usage static variable in android,
some said it is okay and otherwise said don't,
because the static address's memory and address will not deleted until the app is terminate.
For me, it's okay...if the static variable only hold small memory.

Okay, here the code....

Store the data :




Retrieve the data on next activity : 




StaticMode.java : 




Benefit of using static class is application can call the variable from any activity.


Singleton


Singleton behaviour same as static variable, because both use static parameter.
The difference is singleton use static object, to retrieve the object need call function newInstance() or getInstance().

Here the code for singleton,

Store the data :




Retrieve the data on next activity : 




SingletonMode.java :



The benefit same like static variable, just singleton is more organize than static variable.


Preferences


Preferences also known as SharedPreferences, is  a storage method that provided by android. It main purpose to save a session or local data to use on next running
In other word, the data in Preferences are permanent until apps remove or delete it. but it kind a dangerous if you use SharedPreferences class provided by android to store an important data
It is because ROOT device can view the data store in preferences. 
So, my suggestion is please use third-party Preferences class like SecurePrefences to store in important data it more save.

btw, here the code to use SharedPreferences.

Store the data :




Retrieve the data on next activity : 




PreferencesMode.java :



SharedPreferences suitable to store single data like number, word and boolean, not an array. For array, please use SQLite for permanent data.


Intent


Intent..just a title...
This most suggested method for passing data, which attach the data to send with intent. 
Almost all kind of data you can send with this method, but for object you need use Serializable.

Store the data :




Retrieve the data on next activity : 


Benefit is the data will disappear with the activity.


Application


Application is not about you mobile app but this a class provided in android.
All activity in application will use same Application Object. If any changes happen at Activity A..
then Activity B also.
Like static...there are two opinions..
one said you can use application to store data..and another said don't...you just made the application need more load time during start-up.
But for me...it is okay if the Application class only store small data and doesn't hold anything during start-up.

Okay, we just go through the code,

Store the data :




Retrieve the data on next activity : 




ApplicationMode.java :



Initialize, for custom made Application class you need assign it in AndroidManifest.xml :




Not all techniques/method above are acceptable and reliable to use in any situation...
I just list all possible option and give a help to someone new in android to decide which method/technique can been use in android....


Okay finish with the code, so, here the project full code and sample running,

Source Code : PassingData.zip

Sample Running:

Input Activity, have five option, what kind of method you want use to send data to next activity.
The listener in MainActivity.java






Below the Output Activity,









If you have problem or something to ask that related to java...
you can email or comment on this post...
I will try to find the solution and share it with you..


you can test it by yourself..
i hope you like this souce code...
and please give some comment....
Created By : Z-man, 2015

No comments:

Post a Comment