Android Navigation Drawer, Fragments, DialogFragment - android

I am lost at how to implement this scenario:
I want to use NavigationDrawer for top-level switching. It will be something like this:
Fragments are used for all the Top Level Views and Lower Level Views.
This means I only have One Activity.
In the TopViews and LowerLevelViews, I may need the user to input something. I use variants of Dialogs which extends DialogFragment.
Under the normal coding pattern, one would define a listener interface in the Dialog which the Activity would implement.
Here comes my problem. I only have 1 activity and I have Three variants of Dialog. I have Three TopViews and Two LowerLevelViews in each TopViews. This brings me a total of Six (3 x 2) Fragment Views.
Any of the views can invoke a Dialog.
Currently my flow is this:
Fragment (view) needs to open Dialog -> Fragment informs Activity and remembers the Fragment -> Fragment instantiate the Dialog -> Dialog is presented
After the user inputs and click OK.
Dialog invokes the Activity's listener method -> Activity retrieve which Fragment initiated the Dialog -> Sends the value back to the Fragment.
How do I nicely send the value for the dialog to the correct view (fragment) since the Dialog only have access to the Activity?
Does the Activity HAVE TO control the flow? Because it is messy.
Not only that, within a Fragment, I have several EditText which invokes to Dialog. This means on top of keeping tab on which Fragment invoke the Dialog in the Activity, I also have to keep tab of which EditText within the Fragment invoke the Dialog.
Have I explained my issue well? Can someone suggest to me a better solution?

Related

How to Prevent Android FragmentStatePagerAdapter from Creating Neighboring Fragments

I am creating an Android app, and I have linked aFragmentStatePagerAdapter to a TabBar inside one of my Activities to allow users to slide from Fragment to Fragment. Each Fragment in this Activity is populated by a REST call, and if it fails, a Dialog will pop-up saying you should try and refresh your information (only the first time you view the Fragment). The problem I am having is that since FragmentStatePagerAdapter creates the neighboring Fragments to the Fragment you are currently on, it is creating the Dialog(s) of said Fragments prematurely (For example if you are on Fragment 3, and Fragment 4 prompts a Dialog, it will show on Fragment 3 instead of Fragment 4). Is there a way to turn off the creation of neighboring Fragments using a FragmentStatePagerAdapter?
Thank You!
You can control the number of neighboring fragments that area created by calling setOffscreenPageLimit, but the minimum is 1.
Instead of creating your Dialogs in the fragments' onCreateView, I would do it setUserVisibleHint instead. That way the dialog is only created when you're certain that the user is on the page.
No i think you can't , Though there is a property for view pager
viewpager.setOffscreenLimit(0) //defaults to 1
but you can try some other solutions
show Dialog in onResume();
make an interface in Acitvity and fire up to it's implementation in Fragment
tab.setOntabselected{
Pageselected(pagenumber)
}

Launching AlertDialog From Fragment

I have an AlertDialog, which is launched from a Fragment. It inflates a "reset password" layout in its View and looks like this:
I'm still trying to figure out the "correct" relationship between Fragments and Activities. My question is, is it ok to launch this type of AlertDialog from the Fragment itself or is it smarter to notify the Activity that a button was clicked in the Fragment and launch it from the Activity? Thank you.
Ideally, you should handle all of your UI relating to that fragment (like your button click) inside the fragment itself and only use the activity as a sort of container or controller to manage your fragments.
In this case, with something as simple as an AlertDialog, I would just pop it open from inside your fragment. However, if you were going to open another full fragment, then I would refer back to the activity via a callback method to open the new fragment.
Just with anything else, there are countless ways you could do it and it would be fine. I just think that way is a sort of "best practice".

How to use dialogs in a MasterDetail android application

I've been following the android developer guide, (specifically the Passing events back to the dialog's host) to create a DialogFragment that will contain an EditText and pass back that text.
I want my DetailFragment to call that Dialog with a button press, but implementing my DialogListener on the fragment is not enough, it must be implemented on the activity, but from the DetailActivity I can't call my button's OnClick (button is on the fragment).
So my DialogListener must be implemented on the activity, but from there I can't get to my buttons, which are on the fragment.
Do my buttons just have to be on the DetailActivity instead of on the fragment for this to work? Or is there another way to go about this?
I think this is what you are looking for
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
you need to send a message to the activity when the button is clicked, then let the activity popup the dialog

Alert Dialogs Over Activity Transitions

Assuming we have two Activities: List Activity and Detail Activity.
We have a "Save" button in Detail Activity which after saving, finishes the activity to go back to the List Activity.
I would like to know if we can have an alert dialog fired from "Save" function of Detail Activity so that it can stay over the transition of moving back to List Activity..
In other words, Can a Dialog exist outside the Activity? My understanding is that a Dialog is a child of an Activity and has to be destroyed if the activity is destroyed..
In iOS this is possible since the Dialogs are attached to the navigation controllers. Is this possible in Android? I am not considering fragments here..
If you simply want to display a message like "your changes have been saved" then in the Android world you would display a Toast. A Toast can even have a custom layout.
Another possibility would be to move your code to Fragments (which would be a good idea anyway) and then have an activity that acts as controller (i.e. creates and swaps the fragments). That should allow you to open a dialog while activating a different fragment.
You can choose two possible options:
You can stop the AlertDialog when the Detail Activity is dismmissed and then show it again in the method onCreate on the List Activity.
Another option is that you use a one single activity rather than two activities, and change the layout of the Detail Activity to the layout of the List Activity when you push the "Save" button.
There isn't an easier way to get what you want.

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