How to create a chooser for fragments in Android? - android

I have 3 Fragments say FragmentA, FragmentB and FragmentC. I have a button on FragmentA. I want a chooser to be opened as when click on the button of FragmentA which will all the user to choose from FragmentB and FragmentC.
By chooser I mean like a intent.createChooser() which gives options to the user to choose a intent from the list.

Fragments should always be designed for reuse, and hence, you should not write code that directly references a Fragment from another Fragment. In other words, the parent ActivityA should manage your Fragment transactions for you.
Let's assume that ActivityA is the parent Activity for FragmentA, FragmentB, and FragmentC. What you could do is define a callback interface inside the fragment and require that the host activity implement it. When the activity receives a callback through the interface, it can share the information with other fragments in the layout as necessary. If you are only ever dealing with one parent Activity to manage all three Fragments, you won't need to create new Intents to start new Activitys... instead, your parent ActivityA should make use of the FragmentManager and simply fill its layout with the desired fragment when an event callback is received.
There are tons of examples that demonstrate how to use Fragments correctly on the Android developers site. I suggest you also take a look at the documentation here.
Finally, I'd just like to point out that it seems like a tabbed design would work great for your situation (i.e. filling the ActionBar with three tabs that allow quick navigation between Fragments). Check out this sample code for more information.
Let me know if that helped! If not, leave a comment and I can clarify.

Related

How can I show two activities on the same screen?

I am trying to find a way to show two activities on the same screen, I have two activities main and content activity, the main activity has a mini-player that's implemented and only works in an activity and the content activity has some implementations that also only work in an activity so the solution of converting them into a fragment does not work for me. I need to find a way to show the mini-player in the main activity inside of the content activity. I have looked online and none of the solutions so far have been working for me!
You have 2 ways to complete task.
Fragments, They can do same things as activity. Its lite weight activity class with more flexible to attach and detach from the screen.
Views, If you think your task can be done by only activity then use views for it. you can apply different animations and show/hide feature to display content on the screen within single activity.
Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity. Where as the complete screen with which user interacts is called as activity. An activity can contain multiple fragments.Fragments are mostly a sub part of an activity.
From document
For example, a news application can use one fragment to show a list of
articles on the left and another fragment to display an article on the
right—both fragments appear in one activity, side by side, and each
fragment has its own set of lifecycle callback methods and handle
their own user input events. Thus, instead of using one activity to
select an article and another activity to read the article, the user
can select an article and read it all within the same activity
Use Fragment.Fragment are Runs inside the Activity.
you can display multiple fragment in same time.
Refer Fragments
Create a fragment
At first, Android system can show only one activity on the screen.
You should not use Activity.
Please use Fragment. You can use multiple fragment on the same screen.

Android Navigation Drawer with many fragments

I've been struggling with this for a while now, I want to start off with a diagram of my problem:
The three navigation drawer buttons are part of my Base Activity. Each purple block below the three buttons are fragments, and the descendants of each of those blocks are in turn fragments. I'll use the master and detail fragments as a demonstration of the issue I am having...The user clicks the nav drawer button, which opens the master fragment that hosts a list of articles. Once a user clicks one of those articles, I then open the detail fragment inside of the master fragment. So If I find myself at the detail, and I decide to open the nav drawer and click the third button for instance, then click the second button again, I want the detail to be open still, and if I hit the phone's back button I want it to move back to the master fragment, and end there. Any tips will be helpful, as I am probably going to use a similar pattern for the first button, it's main fragment and it's children fragments as well.
In my opinion, the cleanest way to handle what you are describing would be to have three separate FragmentActivity classes that implement the DrawerLayout instead of one monolithic BaseActivity.
Each button in the drawer should start it's respective FragmentActivity using launchMode singleTask. This ensures that you launch the same activity instance each time, instead of a new one, which will maintain your back stack for each activity as you switch between them using the drawer buttons. See Android Developer Guide Activity:launchMode for more details.
Each of the three FragmentActivity instances should be responsible for starting and managing it's fragments using listener interfaces. For example, where you have your Master fragment opening a Detail fragment directly, you should instead have your Master fragment tell it's FragmentActivity that it needs to open the Detail fragment. See Android Developer Guide: Communicating with Other Fragments for recommended practices in implementing this type of decoupled communication between FragmentActivity and Fragment. It will make your life much easier down the road when you want different layouts for tablets, etc.
Each of the three main drawer "tasks" seem to be unique enough that isolating each within it's own FragmentActivity seems the best way to implement what you are trying to do. You can apply this same approach for each of your main sections.
I had the same problem and didn't want to go to multiple activities since that would complicate the back navigation of my application. Your fragments won't save their state automatically unless the activity lifecycle events are being called.
In our case those don't happen since we're not leaving the activity. You can use FragmentManager's saveFragmentState on the fragment you are replacing to manually trigger the state saving and get a Fragment.SavedState object. You can keep a list of your SavedState objects and when pushing a fragment check if you have a saved state for it. If so you can call Fragment setInitialSavedState which will cause your fragment to load the previous state.
Now in my app as a user toggles between fragments with their own child fragments the state is retained when they come back.

Access to the parent activity of a fragment

I have an activity that extends SherlockFragmentActivity. Clicking on any button in this activity makes that frame replaced with a fragment. Fragment contains a ViewPager that a ListView is in it. OnListItemClick event is located within the fragment. when clicking on any of the items in the ListView it makes the new fragment open, and this continues to be hierarchical. To open the second and third fragment and etc, Activity does not have control to replace New Fragment.
My question is:
How can I when I'm in a Fragment, come back to parent activity of this fragment?
Thanks
Have you ever gone through the official documentation for using Fragments in Android?
Please go through it atleast thrice I'd say cause it is the best place to start
learning about fragments. In case you still don't want to do that, here's a sample project(which is also provided by the same site) to start with.

Handling fragment transaction in android.

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.

Android replace fragment or launch new activity with fragment

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).

Categories

Resources