Search code examples
androidasynchronoussemaphoresynchronousandroid-livefolders

How can I emulate getting options synchronously in Android?


Okay, I am trying to write a Live Folder provider, which between being called and creating the folder, first asks the user for some options. It works until I get to the point of trying to wait for the options before creating the folder. I am pretty sure it's due to how I am trying to wait on the options activity.

I am attempting to wait on the options via a 1-slot semaphore. Here is my basic semi-pseudo code:

package mypackage

import <all-my-imports>

public class Folder extends Activity {
    Intent getOptions = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Intent intent = getIntent();
        final String action = intent.getAction();

        if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {           
            try {
                CoreLib.Blocker().acquire();                
                getOptions = new Intent(this, Options.class);
                startActivityForResult(getOptions, 0);              
                CoreLib.Blocker().acquire();

                if(getOptions == null) { 
                    throw new Exception("Live folder canceled."); 
                }

                <create-live-folder-from-options>

                setResult(RESULT_CANCELED);
                finish();
            } catch (Exception e) {
                CoreLib.showToast(e.getMessage(), Toast.LENGTH_LONG);

                setResult(RESULT_CANCELED);
                finish();
            }
        } else {
            setResult(RESULT_CANCELED);
            finish();
        }
    }

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        getOptions = intent;
        CoreLib.Blocker().release();
    }   
}

CoreLib.Blocker() is a semaphore which is initialized as so: new Semaphore(1, true).

My thinking was that it would acquire the semaphore, then launch the Options activity, it would then try to acquire the semaphore again, which would lock it up until the Options activity returned, where it would then call the release in the onActivityResult, and then the onCreate would continue. It seems to not work though... just hangs up on the second acquire and that is it.

What is the best way to go about getting options from the user, before creating the folder, so that I can use those options in the creation of the folder?

UPDATE: I've also now tried replacing my whole little semaphore logic with an empty while loop after calling the Options activity, waiting on a shared boolean variable which would be flipped by the Options activity when it was finished. But this also sends the app off into oblivion before it can ever get to the Options activity.


Solution

  • Figured this out... seems rather stupid-silly obvious once I figured it out, but here it is...

    Instead of putting my options choices into a separate activity, just put an a layout on the activity which returns the LiveFolder intent to home, and gather my options on there.

    I thought you had to setResult(RESULT_OK, createLiveFolder()) in the onCreate override, didn't realize you could delay it until a button- or list-click. Once I figured that out, it all fell into place.