Im relatively new to android so im trying my hand at what i though would be an easy-ish app but I've ran into an issue to do with view/activity flow that i cant get an understanding on.
i have a fragmentActivity that uses a Viewpager to create tabs, each of those tabs is its own fragment class, thats all fine and working, but now i need to have one of the tabs display a list, when selected it takes you to another "view", my problem is how to create the first list and then how to handle tha clicking of an item in that list to take you to the new view so that the tabs stay in place and the back button doesn't exit the app.
currently ive swapped out the fragment with a list fragment that uses an arrayAdapter to build itself, this has worked as far as the list goes but i cant for the life of me figure out how to utelise its onclick() method to move on the the next screen, without as i said losing the tabs or having the back button simply exit.
so im not sure if A, the list fragment is the way to go, or B if it is how to move on to a new screen correctly
i can post code if needed but its a very general implementation of the classes mentioned so im not sure code will help
The callback you want to handle a click on an item in the list is onListItemClick.
As far as presenting a new screen, you can use a FragmentTransaction to replace your fragment with a new one, which gets a little hairy if you're doing this inside of a ViewPager. The code would look something like this:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.some_containging_view, new SomeFragment())
.addToBackStack(null).commit();
Otherwise, you could simply launch a new Activity, which is simpler, but will mean that the tabs won't be present on the new screen unless you duplicate them there.
An elegant solution was finally found here, it uses a wrapper fragment around my list fragment so the pager is just concerned with the wrapper, meanwhile within the wrapper i can perform fragment transactions to my hearts desire, one small note the linked code uses getChildFragmentManager() that needs to be changed to regular fragmentmanager() from support.app for the backstack stuff to work.
Related
I would like to create a good base for my app and have it working efficiently. I am a bit lost still with understanding the relationships between fragments and activities (in the basic form I get it, but in cases like the one I need to deal with now I am not sure what to do).
Currently I have 3 fragments in a View Pager, very simple stuff. one of them is for gallery, one of them is for profile and one of them is for forum (imagine the question board here on SO).
Now, inside the forum fragment I have a FAB that should open a new page for asking a new question.
What I am wondering about is, should the new question page be a fragment or an activity? Also, when a question is clicked I would like its thread to open with all the comments. That should be a fragment as well correct?
If fragment, where do I load it into? The View Pager? If so how? Can the view pager host a fragment temporarily? Because the fragment should only be visible when the button has been clicked. Should I place the fragments inside of the forum fragment so it'll serve as it's parent fragment?
I can make it as an activity, but by what I read and know about fragments, it makes sense that it should be a fragment because it is part of the same activity, but when that is that case I am not sure how to do it.
I would actually not use a ViewPager in your case. I use ViewPagers for a series of related screens that deal with a very specific task. For example - a wizard that details instructions where each Fragment might represent a step and it's useful for the user to be able to slide back and forth between instructions.
Given your requirements I would still host everything under one Activity and the individual pages as Fragments inside that activity. Then maybe use something like a bottom nav bar to navigate between the profile, gallery and forum. The screen that is launched by the FAB and another one for viewing the thread of a forum post should be also be separate Fragments as well.
This might be a good opportunity for you to try out the navigation framework under Jetpack. It makes these kinds of things a lot easier to visualize.
See here Android Navigation Framework
There are already some open threads about this subject but mine is a little different and I can't seem to find out how to do it. This is my problem:
I'm creating a listview (recyclerview) inside a fragment which I put inside my Viewpager. This listview represent a timeline/agenda of the current day. Whenever the user swipe left or right, the exact same listview fragment needs to pop up with different data (data from the day before or after). Whenever you click on one of the items in the listview, the fragment is replaced by a detailed fragment.
This doesn't sounds so hard but here comes the tricky part:
All the examples I see on the internet show different Fragment classes inside the Viewpager which makes it possible to do a transaction from the listfragment to the detailed fragment, and all the examples that use multiple fragments of the same class don't do a transaction to a detailed fragment.
Whenever I'm trying to do the transaction to the detailed fragment, it looks like it can't find the proper fragment container (since there are 3 of the same now). I'm pretty sure of it since the new fragment does appear but the old fragment stays visibile underneath (when I don't use the viewpager it works fine).
If anyone could provide me with a simple example of a viewpager with multiple fragments of the same class that have a transaction to a detailed fragment you would save my day.
Regards
Scenario:
I have a fragment which has a ViewPager which contains 5 instances(different) of a single Fragment with different values, each having a listivew that contains some sort of items.
Problem:
The flow goes smooth when I click an item of listView(say on page1) but the moment I come back from there on pressing back (overridden OnBackPressed in the main_activity (will discuss later)), the viewPager fails to load the subFragments.
But when I switch to the 2nd or 3rd page, it gets displayed, and on going back to 1st page, it now gets displayed.
OnBackPressed():
I am maintaining a manual stack here. When I click on an item of ListView, the current Fragment Instance (of the parent Fragment ofcourse) goes into stack. And when user comes back , that instance gets popped off the stack and I replaces it on the activities FrameLayout container.
References:
Fragments not getting recreated by FragmentStatePagerAdapter after coming back to the main Fragment instance
Guys, I am really pissed off here, please help me out.
Frankly, I haven't worked much with fragments.
But it seems like the page is being thrown off the memory every time you switch to other pages. I am not sure but if there is a way for android to manage this and keep the page in its memory persistently, it might solve the problem
Finally I am able to get a workaround for this issue, which included two things:
while replacing the fragment make a call to
ft.replace(R.id.content_frame, frag).addToBackStack(null);
while initiating the ViewPager, the fragment manager to be used is :
mAdapter = new myAdapter(getChildFragmentManager());
Actually the problem is that, android fails to recognise and load the older instance of the fragment unless you explicitly add that instance to the backstack. So after adding it to BackStack, there is practically no need to maintain your own manual Stack, but you still can do that.
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.
I am still unsure of best design practices using fragments. I've looked at the dev docs at:
http://developer.android.com/guide/components/fragments.html
There seems to be two ways that a new screen can be made, in a single pane layout at least. Let's say I have a button inside one fragment and I want it to show a new view when clicked. Should I be using the original activity and replace with a FragmentTransaction or should I have the original activity launch an intent to a new activity that displays that fragment. I am pretty sure both can work. I'm more wondering about design practice. Or should I use a dialogfragment?
If it matters, the second fragment needs to pass information back to the original fragment at some point.
If all you need to do is return some data to the fragment I would probably use Dialog.
However, for switching fragments, its much better to use a FragmentTransaction to change Fragments, that way you don't need a new Activity (one of the main positives of using Fragments).