0

What I want to do is, I guess at least, refresh a ListView when DialogFragment is being dismissed. Whole refresh process is in my AsyncTask. I found actually a lot questions/answers but nothing helpful in my case.

I tried:

1)onDismiss in my DialogFragment, get instance of AsyncTask local class and execute it. But I got bunch of errors and I think because I tryed to create a new Activity and each time dialog is dismissed, what actually makes no sense and futhermore eats memory a lot.

@Override
    public void onDismiss(DialogInterface dialog) {
        MainActivity outterObject = new MainActivity();
        MainActivity.LoadApplications buffer = outterObject.new LoadApplications();
        buffer.execute();
    }

2)onResume in MainActivity, because I thought Activity goes into state 'paused' when Dialog appears. But it only refreshes ListView when I close and open my app again.

@Override
    protected void onResume() {
        super.onResume();
        new LoadApplications().execute();
    }
Bas
  • 469
  • 8
  • 22
  • Why not just call the asynctask from the onclick event of the dialog fragment? – SoulRayder May 26 '14 at 08:59
  • My AsyncTask class does a lot of work before ListView is being displayed and you can choose a lot of application which are installed on your device. So each time you click on app would make user wait like 3-4 seconds before he can select more apps. And It would also be my 1st case where I need to call local class inside of MainActivity. –  May 26 '14 at 09:04
  • Need more info as to what exactly you are upto? – zIronManBox May 26 '14 at 09:16
  • Update ListView on my main activity once dialogfragmet is dismissed. –  May 26 '14 at 09:28
  • U can just display a "spinning wheel" by instantiating a progress dialog in your asynctask telling the user that processing is being done , when the processing is done, u can proceed to your listview – SoulRayder May 26 '14 at 09:47

2 Answers2

1

Instantiating an Activity the way you do in your onDismiss method is just wrong. There should never be the need to do that.

It looks like you need a way to communicate between your DialogFragment and Activity. This exact topic is presented clearly in the docs. In you case, this could be a potential implementation:

interface Refresher {
  void onRefresh();
}

class MainActivity extends Activity implements Refresher {

  // ... 

  void onRefresh() {
    new LoadApplications().execute();
  }  

}

class MyDialogFragment extends DialogFragment {

  private Refresher mRefresher;

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
      mRefresher = (Refresher) activity;
    } catch (ClassCastException ex) {
      throw new ClassCastException("Activity must implement Refresher interface!");
    }
  }

  @Override
  public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    mRefresher.onRefresh();
  }

} 
Tadej
  • 2,901
  • 1
  • 17
  • 23
  • Is there way to use "implements" command more than once? Maybe im already implementing something or may implement anything in the future. –  May 26 '14 at 10:03
  • 1
    Your `Activity` may implement as many interfaces as you want. If `A`, `B` and `C` are interfaces you want to use in your `MainActivity`, this is the syntax: `class MainActivity extends Activity implements A, B, C`. – Tadej May 26 '14 at 10:07
  • Thanks a lot once again. exactly what i was looking for! –  May 26 '14 at 10:55
0

Try this in your Dialog

public class CustomDialogFragment extends DialogFragment
{

    private OnDismissListener onDismissListener;


    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState)
    {
        Dialog dialog = super.onCreateDialog(savedInstanceState);        
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener()
        {
            @Override
            public void onDismiss(final DialogInterface dialog)
            {
                if (onDismissListener != null)
                    onDismissListener.onDismiss();
            }
        });
        return dialog;
    }

    public interface OnDismissListener
    {
        public void onDismiss();
    }

    public void setOnDismissListener(final OnDismissListener onDismissListener)
    {
        this.onDismissListener = onDismissListener;
    }
}

and call in yout activity

myCustomDialogFragment.setOnDismissListener(new FAQDialog.OnDismissListener() {
                    @Override
                    public void onDismiss()
                    {

                    }
                });
andreich
  • 1,120
  • 1
  • 11
  • 28