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
Related
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".
I have an Activity having multiple fragments of tabs, on pressing "Running total " button present at the bottom, the UI of that particular tab hides and another layout which displays a table,is shown. I want to display the main UI of the tab and hide that table layout on pressing device's back button. I can't use BackStack as I'm using horizonal naviagtion(tabs). How to achieve this?
You could simply override the onBackPressed() in your activity and write the logic there to handle what you want to do.
If you going to manipulate fragment during onBackPressed, it could be done by overriding onBackPressed in the Activity itself. But this would mess up the Fragment-Activity communication. It's better to have onBackPressed in the Fragment also.
A simple google search will give you multiple links on how to have onBackPressed in Fragment.
Follow the below link for how to implement this: http://vinsol.com/blog/2014/10/01/handling-back-button-press-inside-fragments/
When I press a button in my Activity, a DialogFragment pops up. Inside the dialog fragment, there is a RecyclerView that looks a like a normal list view.
The behaviour I want is that when I press on row n of the RecyclerView, the DialogFragment closes and the Activity does something based on the value associated with row n.
It seems that the ViewHolder has to implement the OnClickListener interface. When a row is clicked, the ViewHolder's delegate should do something. The delegate should be the DialogFragment. And the DialogFragment in turn talks with the Activity to which it is attached.
If this is the case, the ViewHolder has to ask the DialogFragment to do something, and the DialogFragment asks the Activity to do something. Is this the right approach? How do I pass the reference of the DialogFragment to onCreateViewHolder()? Should the Adapter keep a reference to the DialogFragment?
Yes, you are moving in the right direction. Pass the DialogFragment's reference in the constructor of the adapter. Once you have the reference and the desired click event fires, call getActivity() on the dialog's reference to get a reference to the activity. Then you can do whatever you want in the activity. Also, I suggest you implement listeners using interfaces. What you want to do is keep the DialogFragment invisible to the underlying activity and your adapter loosely coupled to the DialogFragment, and interfaces will help in that case.
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?
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.