4

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
HendraWD
  • 2,984
  • 2
  • 31
  • 45
AEMLoviji
  • 3,217
  • 9
  • 37
  • 61
  • 3
    Please share crash logs – ρяσѕρєя K Feb 11 '16 at 06:56
  • Probably `mAdditionalInfo = (List) getArguments().getSerializable("add_info");` line causing issue and also share crash logs of Application instead of whole device – ρяσѕρєя K Feb 11 '16 at 07:02
  • 1
    That doesn't look like a crash log. Also, when pasting a log, please format it as code to make it easier to read. – Doug Stevenson Feb 11 '16 at 07:04
  • sorry, but i can not send you any crash, there something goes not normally, it just show this logs, and on memory TAB i see that using Memory is growing. – AEMLoviji Feb 11 '16 at 07:05
  • `there something goes not normally` Unless you can provide the actual log which everyone is asking for, it's almost impossible to get help. Are you using Android Studio? In **Android Monitor**, try changing the dropdown at far right to "show only selected application" & if it still doesn't appear then change the dropdown towards it left to something "Warn", "Error" or "Assert". – Sufian Feb 11 '16 at 07:16
  • yes, i am using Android Studio. Tried to change dworpdow. And no Erroor no Assert is showing only shows logs that i send. – AEMLoviji Feb 11 '16 at 07:45
  • Restart your logcat and see if it gives something (see [this SO question](http://stackoverflow.com/questions/17432358/android-studio-logcat-nothing-to-show)). If not, launch your app and cause the crash again and then check logcat. – Sufian Feb 11 '16 at 07:50
  • 1
    i will create simple app to illustrate this problem. Thanks – AEMLoviji Feb 11 '16 at 07:54
  • I needed a "please confirm ..." dialog today and so I just implemented it using your DialogFragment code and encountered no problem. One difference: I tested with a List. So I think it's quite likely that your issue is somehow tied to the List you are using. Other possible differences: the imports (for me: android.app.Dialog, android.app.AlertDialog, android.support.v4.app.DialogFragment ) – Bö macht Blau Feb 11 '16 at 09:28

2 Answers2

2

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();
}
AEMLoviji
  • 3,217
  • 9
  • 37
  • 61
0

Initialize your Dialogue fragment as below..

AdditionalInfoFragment dialog = new AdditionalInfoFragment();

change ur dialog fragment class ass follows

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_additional_info, null);
 return v;
}
Rishad Appat
  • 1,786
  • 1
  • 15
  • 30