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".
Related
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?
I've implemented a NavigationDrawer within the MainActivity which containts two Fragment. The second Fragment contains a ListView that opens a new Activity (onItemClick) that shows the according detailed data. (I guess that's pretty much the master/detail flow).
When I use the up button to navigate back I got shown the first fragment and not the second fragment with the ListView.
Any ideas how to solve that problem?
Make method in MainActivity for example setFragment(int whichFragment);
and set fragment you want in it, you should already have code that do that and than call that method in onBackPressed() method.
For your question about another fragment, well it depends on how your master/detail flow is suppose to work, it is not a problem to use another activity if you dont need left menu any more, but if you do need left menu then use another fragment.
Best regards
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.
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.
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.