I am developing an Android application and I have the standard list and details implemented with fragments. In case that only one pane is shown, I want to dynamically set the action bar title after transition depending on the selected item. Where should I do that?
I have already put the index of the selected item in the intent and forwarded it to the fragment, so both detail activity and detail fragment know about it. Logically, I would set the title in the fragment, otherwise I have to deal with my content in the activity. However, I also have to check whether I am in one or two pane mode which is done in the activity. So, where is the right place for updating the title?
For me, updating the title (and everything related to the content) belongs to the fragment. I was first thinking about using an interface for invoking an updateTitle method of the fragment from the activity. Because of the high complexity for such a simple thing, I rejected the idea. Instead, I check in the fragment whether a detail fragment is displayed, in order to know whether there are one or two panes on the screen. After that, I simply invoke getActivity().setTitle(title) from the fragment with my title as parameter. This seems to be much shorter.
I would still appreciate each answer telling me what is the best practice in respect to this. But for now, my problem is solved.
Related
I have a MainActivity in my Android Application where I am showing a list of items in a RecyclerView.
Each Item can have multiple sub items, and sub items can have its own child items too (like a tree).
I want to show the same activity for main and sub items, but with an Back Arrow on ActionBar.
Is it possible to reuse MainActivity?
yes, you can do this using fragments. Instead of showing lists in MainActivity take a fragment container in MainActivity and create fragments where you can show your lists and subitems according to your requirement.
By checking fragment instance in MainActivity, you can show and hide backarrow of actionbar.
Check this
Yes, it is possible to reuse MainActivity. Each time you call startActivity without special launchMode or flags, a new instance of MainActivity will be created and it is considered an absolutely new one.
I have encountered a similar problem while working on a File Manager application. So What I did is created a FolderNavigator stack that helped me in keeping the track of users current position using which I showed breadcrumbs at the top. And I hooked my fragment with this navigator so as soon as the peek of stack changes my folder gets notified and it will load the data of the peek of the stack. When I press back I will just pop the stack and as my fragment is already hooked with the peek of stack it will show the respective data.
You can use fragments to fulfill your requirements.
From this SO answer:
Fragments are more of a UI benefit in my opinion. It's convenient for the user sometimes to see two different views of two different classes on the same screen.
For more about fragments, read this document.
I am currently writing a drawer layout as my main layout, with an embedded FrameLayout that I will be using to hold each “page” when an item on the drawer is clicked on. When the app first starts, an initial fragment will be show. Other fragments may be added/replaced later which is fine, however, my problem is that when the user clicks the back button on the very first “initial fragment”, I don’t want that particular fragment to be removed from the layout. Currently, it is being removed and it’s just showing a drawer layout with no other content (which makes sense). I want the app to automatically exit if the initial fragment was the last one showing and the back button is pressed, instead of removing that initial fragment and then after another back press, then it exits.
Things I have thought of doing:
Not adding the first fragment to the backstack. (If I do this, I can compare it with the classname of the fragment which is a somewhat longer string, or I can use a boolean value for once the first fragment has been placed (and not added to backstack), the boolean is set which allows the fragments to now be added.
Overriding the onBackPressed function of the activity
Does anyone have a suggested way of doing this or can think of a better way? Thanks
The first bullet point sounds the cleanest. You have no other need to handle conditions when back is hit, correct? If that's the case, it's less lines of code (removing one as opposed to adding several) and you get to keep default Activity methods as is.
I know that's not exactly what you asked, but I think the first bullet point is so clean, that I just wouldn't try something else.
I have implemented same in one of the app using my own Stack of fragment. and also implemented onBackPressed method.
Every time when user clicks on item in drawer i add fragment in stack and in back press once its length is 1 I finish the activity with message.
On item click -- Add/replace fragment in container.
OnBackPressed -- Pop fragments from stack and once its last one i finish activity.
Hope this can give you another option to consider.
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?
Hi I am developing android application in which I am using one activity and two fragments. Consider same example which google explain like one list view and detail view. on click of list item we are rendering respective detail fragment.
So I learn how to do fragment transaction and i come up with two solutions. One which is standard way which google explain that make one interface and implement that interface into main activity. And do fragment transaction there inside main activity.
I tried with another way. when I click on list item inside click listener instead of calling interface I change fragment inside my list fragment only and its working fine.
So i want to know what is difference between those to methods. changing fragment from main activity and changing it from fragment only.
What kind of problem i will face if i implement with second method.i.e. changing from fragment only.
Need Help. Thank you.
What kind of problem i will face if i implement with second
method.i.e. changing from fragment only.
There isn't an actual problem, it's more of a design discussion. Using the second approach means you're making a very specific fragment, one that on a click on one of its rows will make a transaction with a specific fragment on a specific place of the holder activity. This would be a problem if you plan on reusing this fragment.
Suppose you have this ListFragment and you decided that it should be used in five other activities(with different data). Because it has a very precise action when clicking one of its rows, this fragment will always require the holder activity to have a specific container(where the transaction will be done) along the specific detail fragment with which it was initially used. The problem is that in each of those five activities you may want to use a different fragment when clicking a row of the ListFragment, this would require making changes to the class of the ListFragment.
Now suppose you have the same behavior with the interface approach. As the ListFragment doesn't know or care who handles that click event(as it passes it to whoever registers as the listener) you can simply put the ListFragment in the five activities with no problem(so no changes to it at all). In the interface method of the activity you could then implement the desired behavior with whatever fragment you want and in whatever container setup you want.
please give me advice how to handle following situation..
I have a main Activity with navigation drawer..When the user clicks on items in the drawer, I change currently attached fragment with another one - accordingly to the pressed drawer item.
I also want to handle the situation when the user clicks on the item related to at the same time already attached fragmen and avoid fragment recreate..
So I came up with following solution. I have an property in which I hold the TAG of my current fragment. When the user clicks on any item form the drawer I check the if TAGs are matching and of not I do a switch..I works.
But I have an problem with back stack navigation. I dont know how to change the TAG holding property when the user clicks on the back button. The fragment changes properly, but the TAG property stays the same, so that it all becames broken (when the user clicks on the item he was before, he is not redirected and furthemore after click on the item related to the fragment pushed from back stack it does the recreate:/)
Hope you guys got where is my problem..I dont give here any code. I just think its not necessary, because my problem is not actually in an existing part of my code, but in a hypothetical yet nonexisting one:) I just need to handle the pushed-from-backstack situation..
Thanks in advance!
no need to callback anything!
let's say for example that the layout where your fragments get replace on each transaction is R.id.mycontent
so instead of having the TAG in a field you can, on each click on the drawer, do this:
String currentTag = getSupportFragmentManager.findFragmentById(R.id.mycontent).getTag();
and with this one you compare to what was clicked.