Android replace fragment or launch new activity with fragment - android

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

Related

How to select the activity and the fragment

What scenarios using fragments what scenario using the activity , How can I make sure you when to use the activity when using fragments !
Basically, when you want a fixed portion of the screen and the rest will be changing, you want to use fragments.
If all your app is going to be on different screens, you want to use Activities.
Anyway, at least you will need one activity to hold your fragments.
Hope this helps.
As i know,You need to extend the fragment when you are displaying the view into tabs.
Otherwise you can extend activity.
It depends on your requirement. if u want to expose your toolbar throughout your application you can use fragment both activity and fragment are quite similar . You can also use only one activity remaining things may be your fragment
It depends on UI which you are creating.
If you are creating Multi-Pane UI then you should use Fragment.
And if you are creating some part of screen which can be reused then for that part you should create Fragment as Fragment is meant for re-usability.
If you are creating stand-alone screen then you should use Activity
For more info please refer this link How to choose Activity or Fragment if both scenarios are possible?

Closing FrameLayout for a Fragment in Android App

I'm working on a simple Android App as a self-learning Project. I've got a lot of it functioning, and I have a main Activity which has a FrameLayout and some RecyclerView and FloatingActionButton stuff going on inside of it.
However, I want to make one of my buttons in my NavigationDrawer open a different view in the FrameLayout using Fragments. Is there a way to do this, sort of making a new Fragment for the RecyclerView and the other stuff and putting the RecyclerView and FloatingActionButton in there?
I tried doing something like this (when the appropriate NavigationDrawer button was clicked):
statsFragment = new StatsFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.rootLayout, statsFragment);
transaction.addToBackStack(null);
transaction.commit();
But this caused my app to crash. Any pointers?
Is your R.id.rootLayout a Layout or a Fragment component?
If your main Activity has a hard coded Fragment (by hard coded i mean declared in your XML), then no, you can't change a hard-coded fragment content with FragmentTransaction.
More information on this question's answer.
Finally, you can't put your components from one Activity inside a new Fragment/Activity since each component belongs to one and only one Context.
See, Activity extends Context. When you get a reference to a component declared in your XML by using Activity.findViewById(int), you are actually making a pointer to a determined View from this Activity's layout, defined in your onCreatemethod with setContentView(id). You can't take this pointer and throw it to another Fragment or Activity within your application without expecting any problems. (Well, maybe you can, but it's totally against the best practices of Android Programming).

Multiple fragments or nested fragments

I am writing an android app that will have a number of different screens that I would like to swipe between, each screen will be a full page except for action bar header. On each screen there is the ability to open up another screen which will also be multiple screens that I would like swipeable. What is the best way to handle this. Do I have one fragment manager that holds all the screens and handle the onPageScrollStateChanged to only allow swipes between the current accessable screens or would I be best off nesting the fragments. I hope the above makes sense.
Thanks in advance
Sounds like you want to use a ViewPager to to swipe between views (Fragment extends View)
You could either:
In a single activity use a FragmentManager that switches between the parent and child Fragments, each with their own ViewPager and nested Fragments
Start a new activity to hold each ViewPager
Both are valid, if the Fragments need to communicate with each other or the Activity option one might suit the project needs better.
For the swiping between views you indeed need a ViewPager
For the nested fragments I would use a wrapper. I struggled a lot with fragments and found that this is the best way. A wrapper is very simple. This is just a fragment that holds other fragments. In the onCreate() of this fragment you get the childFragmentManager and add the fragment you originionally wanted to add. If you want to go to a new fragment you simply get the childFragmentManager again and replace the current item. This way you have nested fragment. You can add this to the backstack in order to get back navigation, but you need to override onBackPressed() inside your activity and call the method popBackStack() from the fragmentManager in order to get the first fragment back.
If you have any questions, comment below.

Android veiwpager with fragments view flow issues

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.

How to create a chooser for fragments in 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.

Categories

Resources