0

I have an activity that popup instructions the first time the app runs ( In a DialogFragment).

I want to stop the execution of code while user don't dismiss the Dialog.

Is it posible to do it???

EDIT: I try to use CountDownLatch but it is freezing my UI, and the Dialog is not showing.

Activity :

private CountDownLatch startSignal;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // open tutorial if need
    startSignal = new CountDownLatch(1);
        TutorialDialog tutoDial = new TutorialDialog();
    tutoDial.show(getSupportFragmentManager(), "tuto");
    try {
        toast("aguardando");
        startSignal.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}

Dialog ( I just put the needed methods) :

public class TutorialDialog extends DialogFragment { private final String TAG = getClass().getSimpleName(); MapActivity parentActivity; SharedPreferences preferences;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    parentActivity = ((MapActivity) activity);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//      setStyle(DialogFragment.STYLE_NORMAL, 0);
    AlertDialog.Builder principal = new AlertDialog.Builder(getActivity());
    principal.setInverseBackgroundForced(true);
//      principal.setTitle(R.string.report);
    View view = getActivity().getLayoutInflater().inflate(
            R.layout.tutorial, null);
    TextView ok = (TextView)view.findViewById(R.id.ok);


    ok.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            parentActivity.resumeCode();  
            dismiss();
        }
    });
    AlertDialog ad = principal.create();
    return ad;

}

}

And in the Activity, the method resumeCode():

public void resumeCode(){ startSignal.countDown(); toast("whats next"); }

EDIT 2: I implement it several times. My last implementation should be the one, but it still don't work. I will try to explain how it goes.

mainActivity ( Main thread) launch the dialogFragment:

TutorialDialog tutoDial = new TutorialDialog();
        tutoDial.setCancelable(true);
        tutoDial.show(getSupportFragmentManager(), "tuto");

Then, in another thread, I run await() in onPreExecute():

public class GetPlaces extends AsyncTask> {

private ProgressDialog dialog;
    // I put it public so that I can reach it from the DialogFragment
public static CountDownLatch startSignal = new CountDownLatch(1);  

@Override protected void onPreExecute() { super.onPreExecute(); try { startSignal.await(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } dialog = new ProgressDialog(context); dialog.setCancelable(false); dialog.setMessage(context.getString(R.string.loading)); dialog.isIndeterminate(); dialog.setProgressStyle(R.style.dialogInput); dialog.show(); }

This way, it should not run before the startSignal variable reach 0

Then, In my FragmentDialog, when the dialog dismiss, I run :

    noTuto.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // DISMISS DIALOG
            startSignal.countDown();
            dismiss();
        }
    });

So in theory, it should work, but in the ugly reality, my FragmentDialog freeze...

What I'm sure : 1 - there is no startSignal.await(); in mainActivity that should freeze UI anymore 2 - The startSignal variable is define in another thread:

public class GetPlaces extends AsyncTask<Void, Void, ArrayList<Place>>

so, if anything freeze, it should be this thread, but I don't understand why my dialog is the one which is freezing. Right now, my dialog does not receive clicks anymore...

4
  • The reason your dialog is not showing, is because you are calling startSignal.await(); in UI thread. Your UI thread is blocked from execution. That's why your UI is freeze, and there is no chance for your Dialog to show up. Dec 11, 2013 at 1:53
  • But even if I put it on the onResume() method of the Dialog ( it should display first activity at least, and in onResume, render should already have been done), always freezing.
    – Juliatzin
    Dec 11, 2013 at 2:11
  • Don't call startSignal.await() in UI thread. (onResume to execute by UI thread) Perhaps you should re-phrase your code without using CountDownLatch. Make a clear example on what you are trying to achieve, and what stops you from achieving that. Dec 11, 2013 at 2:35
  • So, If I understand well, I should run my DialogFragment in an new thread, block the main thread, and as my Dialog is in a new thread, I will be able to click something that free my UI, am I right???
    – Juliatzin
    Dec 11, 2013 at 16:54

1 Answer 1

1

Use CountDownLatch startSignal = new CountDownLatch(1);

Your code will usually be executed in user thread. Make your user thread code startSignal.await(); so that it can pause its execution.

Your DialogFragment will be executed in UI thread. Make your UI thread code startSignal.countDown(), to resume your user thread code execution.

2
  • my implementation blocked my UI ! I updated my post.
    – Juliatzin
    Dec 11, 2013 at 1:20
  • I guess I'm blocking UI before he can render views. I tried different ways, always the same...
    – Juliatzin
    Dec 11, 2013 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.