I have a dialog fragment using a custom layout with a quite complex View hierarchy. The code for the dialog fragment is more or less similar to the following.
public class CardDetailDialog extends DialogFragment {
public CardDetailDialog() {
setRetainInstance(true);
setStyle(STYLE_NORMAL, android.R.style.Theme_Light);
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.card_detail_dialog, container, false);
/* Modify some view objects ... */
return view;
}
}
Whenever I invoked the show() method for this dialog fragment, I noticed that onCreateView is always called and layout inflation process is repeated. In my app, user might want to show the dialog multiple times during a session and I thought this is inefficient. Is there any way to keep the view / dialog instance across multiple show() invocation? Is it possible to do this using DialogFragment, or do I have to deal directly with Dialog class?
Using a boolean flag seems to do the trick (See the KEY CHANGEs). I override onCreateDialog, but employing the same strategy in onCreateView should work as well (keep a reference to your view you create)
I'm still getting some issues related to Orientation changes, but it may be related to a different issue
public class LogFragment extends DialogFragment{
private boolean isCreated; //KEY CHANGE
private Dialog mDialog; //KEY CHANGE -- to hold onto dialog instance across show()s
public LogFragment() {
setRetainInstance(true); // This keeps the fields across activity lifecycle
isCreated = false; // KEY CHANGE - we create the dialog/view the 1st time
}
#Override
public Dialog onCreateDialog(Bundle inState) {
if (isCreated) return mDialog; // KEY CHANGE - don't recreate, just send it back
View v = View.inflate(getActivity(),R.layout.log_layout,null);
mDialog = new AlertDialog.Builder(getActivity())
...
.create();
isCreated = true; // KEY CHANGE Set the FLAG
return mDialog;
}
Related
I have an Android Activity, from which I want to show a Dialog. It would probably be a custom DialogFragment. Now when the user clicks on specific buttons I want the layout's inside the dialog to change with the data from the previous DialogFragment and so that it would have an ability to also go back to previous Layout.
I dont think there is an easy way to change views inside of the same DialogFragment so what would be the best way to do this?
I have tried doing it in method onViewCreated and when a button is clicked, but nothing happens.
In my activity I call the fragment like this at the moment:
FragmentManager fm = getSupportFragmentManager();
NewDialog newDialog = NewDialog.newInstace(userId, loc, currentId);
newDialog.setNewClickListener(new NewDialog.OnNewClickListener() {
#Override
public void onCancelClicked() {
finishAdd();
}
#Override
public void onAcceptClicked() {
...
}
});
newDialog.show(fm, "new_frag");
And the fragment:
public class NewDeliveryPointDialog extends DialogFragment {
private LayoutInflater inflater;
private ViewGroup container;
public NewDialog(){
}
public static NewDialog newInstace(){
...
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
this.inflater = inflater;
this.container = container;
return inflater.inflate(R.layout.dialog_layout_1, container);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
saveButton.setOnClickListener(v -> {
View.inflate(getContext(), R.layout.dialog_layout_2, container);
view.invalidate();
view.refreshDrawableState();
}
});
}
}
A DialogFragment is not made to have navigation to other fragments within the same dialog.
You basically have these options:
On your button click you close the Dialog and open another Dialog. But this seems odd. If there is so much happening, probably dialogs are not the best shot.
Instead of DialogFragments have another fragment container overlaying the original one (basically what a Dialog fragment does for you). Within the second container you can easily navigate to other fragments and set it to gone when the user finished interaction.
If there are just a few Views in the Dialog, you could consider setting the old ones to gone and the new ones to visible
I think your code didn't work, because container is null. Method onCreateView gives you #Nullable ViewGroup container, which is null for DialogFragment (but non null for Fragment). So when you call View.inflate(getContext(), R.layout.dialog_layout_2, container), it just creates a view in memory and doesn't attach it to container, cause it is null. See LayoutInflater.inflate, cause View.inflate is just a convenience wrapper for this function.
I dont think there is an easy way to change views inside of the same DialogFragment so what would be the best way to do this?
Instead of changing dialog root you can just manipulate child views inside dialog root layout (add, remove them, or change visibility).
Also my advice is to use recommended way to create dialog with custom layout (onCreateDialog + setView), but if you don't want to do that, you can refer view you've created in onCreateView as dialog root.
You can try creating a dialog fragment with an empty shell layout in which you would replace your two different fragments with ChildFragmentManager and regular fragment transactions
passing data between them can be done using the activity's view model since they both live in the same activity.
So add the ShellDialogFragment using the activity's FragmentManager and in the shell fragment class change between NewDialog & NewDeliveryPointDialog on your button click listener with ChildFragmentManager
We have a web service which serves up an XML file via a HTTP Post.
I am downloading and parsing this xml file into an object to populate some views inside a couple of fragments held in a FragmentPagerAdapter. I get this XML file via an AsyncTask and it tells my fragments the process has finished via a listener interface.
From there, I populate the view inside the fragment with data returned from the web service. This is all fine until the orientation changes. From what I understand, the ViewPager's adapter is supposed to retain the fragments it's created, which is fine, and which I want to happen, and I know the fragment's onCreateView method is still called to return the view. I've spent the last day or so hunting through posts here and the Google docs etc and I can't find a concrete method that lets me do what I want to do: retain the fragment, and it's already populated view so that I can simply restore it when the orientation changes and avoid unneccesary calls to the web service.
Some code snippets:
In the main activities onCreate:
mViewPager = (ViewPager) findViewById(R.id.viewpager);
if (mViewPager != null) {
mViewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
}
if (savedInstanceState == null) {
if (CheckCredentials()) {
Refresh(0,0);
} else {
ShowCredentialsDialog(false);
}
}
Refresh method in main activity...
public void Refresh(Integer month, Integer year) {
if (mUpdater == null) {
mUpdater = new UsageUpdater(this);
// mUpdater.setDataListener(this);
}
if (isConnected()) {
mUpdater.Refresh(month, year);
usingCache = false;
mProgress.show();
} else {
mUpdater.RefreshFromCache();
usingCache = true;
}
}
This is the entire Fragment in question, minus some of the UI populating code as it's not important to show the setting of text in textviews etc...
public class SummaryFragment extends Fragment implements Listeners.GetDataListener {
private static final String KEY_UPDATER = "usageupdater";
private UsageUpdater mUpdater;
private Context ctx;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.ctx = activity;
}
private View findViewById(int id) {
return ((Activity)ctx).findViewById(id);
}
public void onGetData() {
// AsyncTask interface method, will be called from onPostExecute.
// Populate view from here
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_usagesummary, container, false);
mUpdater = (UsageUpdater) getArguments().getSerializable(KEY_UPDATER);
mUpdater.setDataListener(this);
return view;
}
}
If I understand any of this 'issue' it's that I'm returning an empty view in onCreateView but I don't know how to retain the fragment, return it's view prepopulated with data and manage all web service calling from the main activity.
In case you can't tell, Android is not a primary language for me and this probably looks a shambles. Any help is appreciated I'm getting rather frustrated.
If you're not using any alternative resources when the Activity is re-created, you could try handling the rotation event yourself by using configChange flags in your AndroidManifest:
<activity
...
android:configChanges="orientation|screenSize"
... />
There is no way to keep the same, pre-populated Views if your Activity is re-created since this would cause a Context leak:
http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-android/
my code:
public class CustomDialogFragment extends SherlockDialogFragment {
/** The system calls this to get the DialogFragment's layout, regardless
of whether it's being displayed as a dialog or an embedded fragment. */
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.p_product_gallery, container, false);
ImageView tttiv=(ImageView)v.findViewById(R.id.test_image);
tttiv.setImageResource(R.drawable.baozi);
return v;
}
/** The system calls this only when creating the layout in a dialog. */
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
mDialog = super.onCreateDialog(savedInstanceState);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setBackgroundDrawable((new ColorDrawable(0x0f000000)));
mPager = (ViewPager) mDialog.findViewById(R.id.aa_pager);
mPager.setAdapter(mAdapter);
mAdapter = new ProductGalleryAdapter(getSupportFragmentManager());
return mDialog;
}
}
The "mPager" always is null.
Can any one provide an example?
tkx!
Unless you are using Android 4.2 (and Android Support Library rev 11), fragments are not supported in fragments...
Fragments within Fragments
Let's say I have this button:
<Button
android:id="#+id/idone"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="D2"
android:onClick="myMeth"/>
I have several times used this to call methods from a layout xml as it calls the method from the activity that inflated such view.
Recently with DialogFragments, well it does not work at all. I keep getting an error telling me that such method does not exist. Where is it then looking for such method? I have added it to the DialogFragment class:
public class myActivity extends DialogFragment {
public DiceDialog() {
// empty constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myDialog, container);
getDialog().setTitle("Hello");
return view;
}
public void myMeth(View view) {
//...
}
As well as in the activity that instantiates the FragmentManager and calls the dialog:
public Class MainActiviry Extends FragmentActivity {
//...
public void onCreate(Bundle savedInstanceState) {
// ..
FragmentManager fm = getSupportFragmentManager();
MyActivity dialog = new AddDiceDialog();
dialog.show(fm, "tag");
}
public void myMeth(View view){
//...
}
And still the messag is that MyMeth is not found.
I have already read that using interfaces and listeners is the correct way to communicate between activity and dialog fragments, but what I am trying to figure out here is where that myMeth call is being made, because well,it is called.
You can implement public myMeth(View view) in your Activity, which will then check for the currently visible Fragment, and call its method.
If you want to use more then one callable method in your Fragment, you can utilize the id's of the calling views and implement a switch, calling a different fragment method according to the id of the View.
I am using a DialogFragment, and while I have successfully set an image to close (i.e. dismiss) the dialog when pressed, I am having a hard time finding the way to dismiss the dialog when the user clicks anywhere outside it, just as it works with normal dialogs. I thought there would be some sort of
dialogFragment.setCanceledOnTouchOutside(true);
call, but I don't see that in the documentation.
Is this possible with DialogFragment at all? Or am I looking in the wrong places? I tried intercepting touch events in the 'parent' activity but apart from not getting any touch event, it didn't seem right to me.
DialogFragment.getDialog().setCanceledOnTouchOutside(true);
Must be called in onCreateView (as Apurv Gupta pointed out).
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
getDialog().setCanceledOnTouchOutside(true);
...
}
/** The system calls this only when creating the layout in a dialog. */
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
Lot of answers here but, the app crash when dialog opens.
Writing getDialog().setCanceledOnTouchOutside(true); inside onCreateView did not work and crashed my app.
(I am using AppCompatActivity as my BaseActivity and android.app.DialogFragment as my Fragment).
What works is either of the two following lines:
getDialog().setCanceledOnTouchOutside(true);
OR
this.getDialog().setCanceledOnTouchOutside(true);
inside onActivityCreated like
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimationZoom;
//getDialog().getWindow().setDimAmount(0.85f);
getDialog().setCanceledOnTouchOutside(true);//See here is the code
}
What not to use:
DialogFragment.getDialog().setCanceledOnTouchOutside(false);
throws following error
And writing the code in onCreateView crashes the App!
Please update the answer if you find something wrong.
If you want to execute some logic when clicking outside of a DialogFragment, just override the onCancel method.
override fun onCancel(dialog: DialogInterface) {
super.onCancel(dialog)
// Do your work here
}
This works fine for Java:
DialogFragment.getDialog().setCanceledOnTouchOutside(false);
I would recommend to use my solution only after trying out above solutions. I have described my solution here. Just to brief, I am checking touch bounds of DialogFragment.getView(). When touch points are outside DialogFragment, I am dismissing the Dialog.
Dialog.SetCanceledOnTouchOutside(true);
Worked for me
My Code
class dlgRegister : DialogFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
....
....
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
Dialog.SetCanceledOnTouchOutside(true);
base.OnActivityCreated(savedInstanceState);
Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation;
}
}
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState)
{
DialogConfermationdialogBinding binding = DialogConfermationdialogBinding.inflate(inflater,container,false);
View view = binding.getRoot();
getDialog().setCanceledOnTouchOutside(false);
return view;
}