I am working on a development of an app, and currently dealing with the following:
- I have a fragment using certain view comprised of buttons and TextFields (say showing user's detail information). Now, I have to call the same fragment (within this fragment) on another instance of data (e.g. say the details of another user, once certain button is clicked), but hiding some parts of the view when the new data are shown (i.e. the data for the new user, when the button is clicked). Considering that, I was wondering what approach should I use for calling the same fragment again, and having partially the same view as before. Could anyone of let me know what would be a good approach to go for?
Related
I am new to android development so forgive if i asked a silly question. Below i am narrating my requirement and the solution I thought of please read and help me if you have any solution.
In my application i have a activity that contain a toolbar on top, different options in the bottom and in the center area a framelayout where i will load fragments that will render different advertisements(Mostly images) based on the options the fragments using viewpager shows multiple images. Check the following image.
Now my problem is let's assume i selected Fashion option with featured sub option then title should be changed to Fashion-Featured and Fragment should show images related to Fashion>featured let's call this fragment fragment1. Now if the user tap on an image then the application will render another fragment lets call it fragment2.
In the fragment2 the selected image will be rendered in large with two buttons More and Stores. Here if the user taps on more button then the thumbnail images of fashion>featured option will be displayed so that user can select another image. If the user taps on store button then the another fragment will load let's call it fragment3.
In fragment3 the stores selling that particular product will be listed.
Now my problem is how can i pass the data between activity to fragment1 then fragment1 to fragment2 then from fragment2 to fragment3 so that the if the user press backbutton still the fragment2 should show the fashion>featured images with the tapped image on large and if the user press another backbutton then fragment1 should show fashion>featured option images. The data passing should be fail safe that is if the app goes in the background then come to foreground then also it should work.
Please help me out. I am planning to implement using sqlite database and reference in shared preference. I think this is not an exceptional requirement other application also handles this kind of requirement so i need a better way to achieve this.
If you would like to pass some data to a new fragment see this answer: How to pass a variable from Activity to Fragment, and pass it back?
If your fragment is already created you can add some method to it and call this method with required argument.
I have a screen split into two halfs
the top one holds the profile data with profile pic and is static, the bottom half is fragments that are navigated through. One of those fragments edits the details of the profile. Now i like to make things hard for myself, so what i want is when the user presses "update", the information is changed at the top instanstly. How on earth do i do that. When the update is pressed..behind the scences the photo info goes through php to phpadmin and a string of the photo path is uploaded ready for retrivel and the bit map is supposed to update the image view.
I was able to update the profile pic on the fragment with picasso when it was one complete fragment so i knwo that set up works.. Long story short how do i update an Image view which shares a screen but not activities or fragment layout
If I understand your question correctly, you would like a user's interaction with a fragment to result in the update of a UI element in that fragment's parent activity.
The short answer is that while it is possible, the fragment should not update the UI element in the Activity directly. This is what is commonly known as "lack of separation of concerns", or in the common parlance, "spaghetti code".
Instead, have the Fragment notify the Activity that the profile image has changed. Then the activity can handle changing the content of the ImageView.
The commonly recommended way of doing this is to create a pair of interfaces through which the Activity and the Fragment communicate. While this way is recommended a lot, it's probably not the best way (lots of code, and error-prone).
I find that the easiest way is to use a message bus (see Otto, GreenRobot, etc.). The Fragment can put a message on the bus, and the Activity can listen to the bus for that incoming message, and take appropriate action when it arrives. This can be used in both directions, if desired.
I'm trying to find something analogous to a FragmentManager.peek() method.
The use case is that I have a need to send screen focus to the currently loaded Fragment. However, I'm finding myself having to keep manual track of what fragment is currently being displayed so that I can tell it to take focus.
Specifically the situation is that I have a menu which has a number of buttons. When a user clicks a button, I need to transfer focus from the button to a specific View within the currently loaded Fragment - but I have no way to know what Fragment is currently loaded, it may be the initial Fragment associated with the menu item, or it may be a different Fragment altogether that the user arrived at having dug deeper into the original Fragment... so my logic has to be
pseudo...
if(currentFragment == navButton.myFragmentType)
currentFragment.sendFocusToYourFirstRelevantElement()
else
navButton.loadMyFragmentType()
My problem is that I don't know what the currently displayed Fragment is, unless I keep track manually, but that's *really ugly.
Hoping there's a more native solution.
You have to track Fragments manually.
Think about how Fragments work - they're designed to be reusable and, notably, there can be more than one on screen at a time. Because of this, it's not as simple as just querying the FragmentManager for the currently displayed Fragment, because it has no knowledge of which currently displayed Fragment you want.
The logic that that knowledge would require is not something that can be put in the FragmentManager because it's very specific to your app.
For more information: What Fragments am I hosting and displaying?
This question is more my way of gauging a better understanding of the "Proper" way to handle this content flow instead of just "whatever works" or the "quickest" solution to code. Im looking for the best on performance and user experience.
The situation:
So my main activity handles my NavigationDrawer and is the basis of the app. The initial view that is loaded (navitem 0) is a Fragment which contains a RecyclerView(custom adapter, list item model, view holder). This list displays data pulled from an XML file and is returned as an Arraylist of Topic objects (each containing 3 strings and an array of Issue objects).
The array of Topic objects are used to populate the listitem w/ a title, desecription and image_name strings. (Not using the Issue array yet).
Here is where you come in ...
I know need to handle the click event on the Topic and display a new list (w/ different adapter) of the specific Issue object array for that Topic.
I'd like to know if its better to replace the current fragment w/ a new fragment for handling the Issue data. Or would it be better to launch a new activity to display the Issue list data.
Keep in mind, i want to ensure that navigation up will return the user to the previous view. ie. when clicking a Topic you should get the Issues for that topic. When going back, the TopicFragment should be displayed w/ its initial list.
If this is confusing you?
The core part of this question is needing to know the proper navigational way of displaying a List that when clicked needs to display another List specific to the parent object. Is fragment to fragment handled by callbacks in the MainActivity the best way, or is Intent'ing to another activity to handle the 2nd list better?
Whether you use a Fragment or an Activity to display the second list, it doesn't matter from a performance standpoint. If I were you, I'd use an Activity: it is always better to use a Fragment only in situations that require the explicit use of Fragments (such as a FragmentTabHost or a ViewPager).
But I do have another suggestion for you. Instead of going to another list, why not display your Issue objects as the child items of an ExpandableListView, and the Topic objects as the parent items ? Then when the user clicks on an Issue child item, go to the detail page containing details of that Issue object. To me, the List->Detail pattern is a far more familiar idiom than a List->List->Detail flow. Its what ExpandableListView was made for.
I have created a tabbed android application using android.support.v4.view.PagerAdapter.
There are about seven tabs in the application and I plan to add more. Each tab contains a lot of controls (TextView, Spinners, Toggle buttons, Check boxes etc.)
On the first tab there is a drop down to select a profile. The user can 'Load' or 'Save' a profile he wants.
A profile contains data for all the controls in the tabs and I need to update the UI controls in all the tabs.
I have all the data loaded from the profile but the UI controls are not getting updated.
There is a 'UpdateUI' function which calls 'set' functions (setText, setChecked etc. for individual controls after finding its view by ID).
I was informed that only three tabs (Previous, Current and Next) are kept in memory so I wrote the application such that the 'UpdateUI' function is called to set UI data only when user swipes to that particular tab (figuring out the active fragment).
Using DDMS logs I saw that the data loaded was proper but the 'setText' or 'setXXXXX' function does not update the fragment tab.
I have also checked several possible issues:
Possibility of 'OnTextChanged' or an 'OnListener' updating the data again.
Made sure 'UpdateUI' is called from UI thread.
Using notify data change and redrawing UI to make sure UI is updated.
I am a novice Java/Android programmer. Please point me in the right direction.
viewpager.setOffscreenPageLimit(size);
you can instantiate all your fragments by setting limit then you can update all widgets inside other fragments.
If my understanding of Android Fragment management is right, once the fragment becomes invisible for user (say some other fragment completely overlayed it or in your case you change tabs) Fragment goes through onPause and possible onStop lifecycle stages, this mean it's kind of hybernated and can't be changed before it get's visible. viewpager.setOffsetPageLimit(size) tells the fragment manager (or rather pageAdapter) how many fragments should be kept hybernated, and I doubt it playing with this will change anything, but let me know if it's, because otherwise the solution may be more complicated.
What I'd do is recreate a fragment every time user gets to see it and pass your profile data to it's constructor (or following better practice newInstance() static method), it will in fact save memory since keeping many fragments there may be overwhelming. Alternatively you can check what profile is chosen everytime fragment is calling it's onResume, and update your controls there.