Passing data from an Activity to its View - android

This is a newbie question. What is the best way to pass an object from an Activity to its View? I know the View has access to its context. Are Intents still the way to go? Or is there something else?
EDIT: Did not know the details about the data mattered :)
The data is user information obtained from a server in the Activity stored in an object. It contains userid, name and other details (mix of longs and strings).
The idea is to use/show the data in the view and possibly modify some of the fields and pass it back to the Activity which will send it to the server. Also I create and launch the View after I have the User information object with me. So after the View is launched it has to somehow get the object when it wants to use it.
P

You can use setTag: http://developer.android.com/reference/android/view/View.html#setTag%28int,%20java.lang.Object%29

I don't know what you exactly mean with passing data form an activity to a view as a view is normally part of an activity.
So I would suggest you start an AsyncTask in the onCreate() method of your activity which downloads the data from the server. When the AsyncTask finished you pass the data pack to a method of you activity which takes the data and fills the corresponding view elements. To get a reference to the views like TextView, EditText and so on you can use the method findViewById().
When the finished editing the data he must press a Button which triggers an OnClickListener that fetches the edited data from the views and starts another AsyncTask to upload the data back to the server.

Related

How can I update item of recyclerview from dialogfragment?

In my app I get some data from server in json. Then I fetch fields from json and fill my RecyclerView list with custom adapter. This list is filled with data about jobs. When I click on a job of my recyclerView I move from hosting fragment to dialogfragment for viewing it. This dialogFragment contains homesUp button after pressing which I return to hosting activity. At this dialogFragment I have button which send request to server about this job for adding it to another DB at server.
But when I press this button and send request to the server then after returning to hosting activity I see that data of selected isn't changed. I have several ways of solving this problem:
Add to my Singleton class variable of ArrayList which will be filled by data of shown job and then I will check whether my Arraylist from Singleton contains this id
another method - notifyItemChanged()
All these methods don't work because my list is filled by old data and I only dismiss dialogFragment and return to fragment which contains the list with old data.
Only one way - send request to server for filling the list again.
So, I need your help or useful advice :)
You need to broadcast changed data from dialog fragment to previous screen. For doing this can use LocalBroadcastManager or rxjava or even live data.
Or create a pagedlistadapter from paging library released in android architecture complement and load data directly from query.
Reference: https://developer.android.com/topic/libraries/architecture/paging

Set data from Activity to it's Fragment at runtime on data updated from server

I have another one question about transfering data from Activity to Fragment.
In my activity I have next situation: one part of UI is situated in Activity, and another (more dynamic part) is situated in Fragment.
The data which I need to populate my UI elements in Activity and Fragment is on server. To get that data, I am sending request to
server in Acvitity's onCreate() method. In Activity's callback method: void onDataLoaded(List<MyObject> dataList) I get data from server.
And in this method I am creating my Fragment and setup data to it. I am passing data to it through the Bundle object duding creation. Everything
is ok with this. But the issue is in next: on network reconnect I need to load data from server to be sure that all data is up to date. And of course I
need to reinitialize data in Activity and Fragment. But I don't want to fully RE-CREATE fragment. I want just to setup new data to it's fields.
How can I do that properly? Is it a good way to to keep reference in my Activity to that Fragment and call some public method: myFragment.SetMyCustomData(List<MyObject> dataList) ?
I understand that the best way to load data from server in my fragments onCreate() method, but I can't split it into two API calls and I need that data in Activity as well.
Thanks.
Well this is not a transfering problem . Anyway you can use Event Bus for easier communication from Activity to Fragment and vice-versa. It is very easy to use . And as for your problem I belive you are not doing what you need in the right time .
https://github.com/greenrobot/EventBus

How to restore Activity with my current Listview Data while switching between activities?

Hello Everyone,
I had do a search on activity retain state.So I am getting to options/solutions for that:-
Using savedInstanceState() and retainInstanceState() methods.
Using Parent Child hierarchy declare in manifest and its working.
(Example Url:-How can I return to a parent activity correctly? I had same problem mention in Note scetion of the answer.That case is match with my problem.
So,I want to retain the activity states just like Whats app(call/chats/contacts fragments).
But In My scenario,I am fetching the contacts from server.So how can I persist my data while switching between chat and my fragment activity?
So while timing to fetch new data of my contact list from server.I want to save ui of my listview/recyclerview with old data previously I had.
Also suggest which method is good from above two methods or need to implement in other way.

Anyway to append to the activity stack without doing a StartActivity?

I'm parsing a huge xml to display a list of titles in a listview in activity A. However the same xml also has details for a list item which needs to be shown in a different view (like list mail subjects/view mail details scenario).
On click event of this list i dont want to load a new activity with a bundle, parse the same xml and show detailed view, while i have the required data in activity A itself.
I figured out a way to hide show layouts in my XML to do this as required, but handling back button is an issue. I can probably do this by capturing back button action, but want to know whether there is a better solution for this.
Like broadcasting an intent to A (from A itself) and somehow managing to add that to the activity stack.
Excuse if there is a duplicate question, couldnt find one when i searched.
BTW, i dont want to do a solution with a database caching.
I would handling the back press. Just use a flag within your activity that tells you in which view you are (so back within the detailed view shows you the overview view).
Another way would be to save the values in your applicationContext. Much easier way to do it than database usage.
Take a look at an answer here: How to declare global variables in Android?
But I would definitely go with handling back presses. I have a solution similar to this where I use the same listview in the layout and instead I use different adapters depending on which detailed view the user is in.
Handling back press is the easiest way to go.
Else you could also pass the information to view as Intent extra to the second activity.
Another possibility is to have a local service running in the background and in charge of loading your XML and offering access to its information in a convenient way.
You can also stuff the XML content in an Application object of your own. However I have had not so great experience with that option in some projects.
I would use a second activity. Pass additional data (like contact list, message details, etc.) to it and display it. How you keep parsed XML in memory is up to you to decide (static member? yuck! but it works).
Now back to original Activity. Does your source XML change a lot? Maybe you can parse it and put all data into a DB so that you could retrieve necessary (and hierarchical) data quicker. This way you do not need to deal with storing lots of data in memory, re-parsing and you could perform search faster.
On click event of this list i dont want to load a new activity with a bundle, parse the same xml and show detailed view, while i have the required data in activity A itself.
Cache the parsed XML in a static data member. Your activities that need the data look at the static data member first, then kick off the parsing if and only if that cache is not there.
IOW, this is not an activity problem, but a data model problem. Do a better job with your data model, and your activities can behave naturally.

Update model objects across different activities

Lets say I have 2 activities:
A: A ListView displaying articles titles. Data is fetched from a web server and converted from XML to a list of ArticleSummary. Only user titles and id are returned by the server. Click on a title starts activity B.
B: A form to edit an article. Article is fetched from server. When the user hits OK, modifications are sent to the server and activity closed.
When the user go back to activity A, I would like to update the article title without any additional web request.
I was thinking about the following solution:
When article is modified, send a broadcast event with article id and new attributes values.
Listen for this event on activity A
Update the ArticleSummary object
notify data changed on ListView
Is there a better approach ?
If you want to have a shared data model between different Activities, you can place it in an extension of the Application class. Or, you can use a singleton. Just reload the data from the shared location when the ListView activity is restarted.
As Fredley alluded to, if you have are communicating with a server you should be sure to do so in a separate background thread.
you can also use startActivityForResult() to launch activity and managed returned data.
Check the "Returning a Result from a Screen" section in the part below.
http://developer.android.com/guide/appendix/faq/commontasks.html

Categories

Resources