Sunday, July 12, 2015

Passing Data Between Activity in Android (Backward)

Continue from previous post, Passing Data Between Activity in Android (Forward)...

In this post, I will explain how return response/data to previous/parent Activity..
Send forward is easy, just attach with intent or store in a storage the start activity...in onCreateView() call the data...because one Activity only have one parent.



But, a parent Activity have one or move child Activity, therefore, before get the data need to identify the response from which Activity. Additional, the previous/parent Activity still alive at the back of current Activity and keep waiting until get response from current Activity.

To receive the response from child Activity, parent activity need override  onActivityResult() function and this the proper way. Below the segment code,



From last previous post, I used startActivity() to start the activity... But to make activity able to send resultCode to parent Activity...
We need replace startActivity() to startActivityForResult()...like code below..

Before


After


Finally, use setResult(RESPONSE_CODE) or setResult(RESPONSE_CODE, INTENT_DATA) function to send the response from child Activity, and here the code,



btw, in storage class I do some modification which I add new variable(lock)....
and here the project full code...

Source Code : PassingData(Backward).zip

Sample Running :


Start Activity




Insert Firstname and Lastname, click "STATIC" button




Click "SAVE" button




EditText hidden, click "SINGLETON" button




Click "EDIT" button




EditText appear



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