AEMLoviji AEMLoviji - 3 months ago 21
Android Question

Andorid custom Dialog Fragment crashes

I want to show Custom AlertDialog.

So i am creating Custom dialog and call it every time, just simply calling it.
When i am creating CustomDialog extended from DialogFragment it crashes, i debug it and show that onCreateDialog method calling many times. And it uses all memory on my device. But when i am simply creating alert dialog inside my activity it works.

So why Dialog not showing, just it is calling onCreateDialog continiusly and Memory using grows?

It works. Directly creating on activiy

@Override
public void onClick(View v) {

LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
View promptsView = layoutInflater.inflate(R.layout.fragment_additional_info, null);

LinearLayout containerLinear = (LinearLayout) promptsView.findViewById(R.id.linearLayoutLoadingProgressbarContainer);
Button buttonOk = (Button) promptsView.findViewById(R.id.buttonOk);

for (KeyValueString keyValueItem : info.getAdditionalInfo().getKeyValueString()) {
if (!keyValueItem.getKey().equals("fee")) {
View viewToAdd = layoutInflater.inflate(R.layout.template_key_value, null);

TextView tvKey = (TextView) viewToAdd.findViewWithTag("key");
tvKey.setText(keyValueItem.getKey());
TextView tvValue = (TextView) viewToAdd.findViewWithTag("value");
tvValue.setText(keyValueItem.getValue());

containerLinear.addView(viewToAdd);
}
}


final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(GuestPayActivity.this);
alertDialogBuilder.setView(promptsView);
alertDialogBuilder.setNegativeButton(R.string.button_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}


It is not working. Creating custom DialogClass and call it on activities

//call

@Override
public void onClick(View v) {

DialogFragment newFragment = AdditionalInfoFragment.newInstance(info.getAdditionalInfo().getKeyValueString());
newFragment.show(getSupportFragmentManager(), "dialog");

}


//custom dialog class

public class AdditionalInfoFragment extends DialogFragment {
private static final String TAG = "AdditionalInfoFragment";
List<KeyValueString> mAdditionalInfo;

public static AdditionalInfoFragment newInstance(List<KeyValueString> additionalInfo) {
AdditionalInfoFragment f = new AdditionalInfoFragment();

Bundle args = new Bundle();
args.putSerializable("add_info", (Serializable) additionalInfo);
f.setArguments(args);

return f;
}



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdditionalInfo = (List<KeyValueString>) getArguments().getSerializable("add_info");

// int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme_Black_NoTitleBar_Fullscreen;
// setStyle(style, theme);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.fragment_additional_info, null);

LinearLayout containerLinear = (LinearLayout) v.findViewById(R.id.linearLayoutLoadingProgressbarContainer);
Button buttonOk = (Button) v.findViewById(R.id.buttonOk);

for (KeyValueString keyValueItem : mAdditionalInfo) {
if (!keyValueItem.getKey().equals("fee")) {
View viewToAdd = getLayoutInflater(savedInstanceState).inflate(R.layout.template_key_value, null);

TextView tvKey = (TextView) viewToAdd.findViewWithTag("key");
tvKey.setText(keyValueItem.getKey());
TextView tvValue = (TextView) viewToAdd.findViewWithTag("value");
tvValue.setText(keyValueItem.getValue());

containerLinear.addView(viewToAdd);
}
}



AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setIcon(R.drawable.ic_bonuses)
.setView(v)
.setTitle(R.string.message_authenticating)
.setPositiveButton(R.string.button_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dismiss();
}
}
);




return builder.create();
}


Here is my logs:

02-11 10:58:58.429 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 17975(561KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 52MB/53MB, paused 5.415ms total 95.850ms
02-11 10:58:58.652 24794-24800/test.app W/art: Suspending all threads took: 74.554ms
02-11 10:58:58.675 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 3160(137KB) AllocSpace objects, 0(0B) LOS objects, 23% free, 53MB/69MB, paused 5.378ms total 153.858ms
02-11 10:59:01.135 24794-24800/test.app W/art: Suspending all threads took: 52.945ms
02-11 10:59:01.139 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 17897(559KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 68MB/69MB, paused 7.785ms total 107.723ms
02-11 10:59:01.446 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 3859(155KB) AllocSpace objects, 0(0B) LOS objects, 18% free, 70MB/86MB, paused 9.483ms total 266.572ms
02-11 10:59:03.868 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 18212(569KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 86MB/86MB, paused 10.623ms total 149.134ms
02-11 10:59:04.166 24794-24800/test.app W/art: Suspending all threads took: 77.074ms
02-11 10:59:04.190 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 3129(123KB) AllocSpace objects, 0(0B) LOS objects, 15% free, 87MB/103MB, paused 10.948ms total 289.089ms
02-11 10:59:06.669 24794-24800/test.app W/art: Suspending all threads took: 9.851ms
02-11 10:59:06.702 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 18118(566KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 102MB/103MB, paused 95.894ms total 245.108ms
02-11 10:59:07.148 24794-24800/test.app W/art: Suspending all threads took: 52.525ms
02-11 10:59:07.157 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 5396(215KB) AllocSpace objects, 0(0B) LOS objects, 13% free, 104MB/120MB, paused 14.787ms total 433.896ms
02-11 10:59:09.540 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 17494(546KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 120MB/120MB, paused 23.833ms total 138.768ms
02-11 10:59:10.245 24794-24800/test.app W/art: Suspending all threads took: 144.977ms
02-11 10:59:10.283 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 5438(206KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 122MB/138MB, paused 17.375ms total 577.503ms
02-11 10:59:12.782 24794-24800/test.app W/art: Suspending all threads took: 176.769ms
02-11 10:59:12.818 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 17243(539KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 137MB/138MB, paused 20.240ms total 213.495ms
02-11 10:59:13.605 24794-24800/test.app W/art: Suspending all threads took: 500.021ms
02-11 10:59:13.616 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 3830(157KB) AllocSpace objects, 0(0B) LOS objects, 10% free, 138MB/154MB, paused 19.801ms total 608.081ms
02-11 10:59:16.121 24794-24800/test.app W/art: Suspending all threads took: 8.391ms
02-11 10:59:16.161 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 18088(565KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 154MB/154MB, paused 21.944ms total 176.228ms
02-11 10:59:16.807 24794-24800/test.app W/art: Suspending all threads took: 194.938ms
02-11 10:59:16.821 24794-24804/test.app W/art: Suspending all threads took: 12.390ms

Answer

I just made some changes on method onCreateDialog.

These lines are replaced:

1.old : LayoutInflater inflater = getActivity().getLayoutInflater(); 
1.new : enter code hereLayoutInflater layoutInflater = LayoutInflater.from(this.getActivity());

2.old : View viewToAdd = getLayoutInflater(savedInstanceState).inflate(R.layout.template_key_value, null);
2.new : View viewToAdd = layoutInflater.inflate(R.layout.template_key_value, null);

Problem with was with second replacement. While you are going to call getLayoutInflater() it somehow try to call onCreateDialog again and again. So simply use first create inflater object(layoutInflater)

My new code seems as below:

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater layoutInflater = LayoutInflater.from(this.getActivity());
    View promptsView = layoutInflater.inflate(R.layout.fragment_additional_info, null);

    LinearLayout containerLinear = (LinearLayout) promptsView.findViewById(R.id.linearLayoutLoadingProgressbarContainer);

    for (KeyValueString keyValueItem : mAdditionalInfo) {
        if (!keyValueItem.getKey().equals("fee")) {
            View viewToAdd = layoutInflater.inflate(R.layout.template_key_value, null);

            TextView tvKey = (TextView) viewToAdd.findViewWithTag("key");
            tvKey.setText(keyValueItem.getKey());
            TextView tvValue = (TextView) viewToAdd.findViewWithTag("value");
            tvValue.setText(keyValueItem.getValue());

            containerLinear.addView(viewToAdd);
        }
    }


    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setIcon(R.drawable.ic_bonuses)
            .setTitle(R.string.title_additional_info)
            .setView(promptsView)
            .setNegativeButton(R.string.button_close, null);


    return builder.create();
}