Created
March 16, 2012 23:46
-
-
Save johncarl81/2053643 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//static way (dont want to do this) | |
public class SlideshowActivity extends Activity{ | |
public static void start(ArrayList<String> images, Song song, Uri videoUri, Context ctx){ | |
Intent intent = new Intent(ctx, SlideshowActivity.class); | |
intent.putExtra(EXTRA_IMAGE_FILE_NAMES, images); | |
intent.putExtra(EXTRA_SELECTED_SONG, song); | |
intent.putExtra(EXTRA_VIDEO_URI, videoUri); | |
ctx.startActivity(intent); | |
} | |
... | |
} | |
// plain factory: | |
public class IntentFactory{ | |
@Inject | |
private Context | |
public void startActvity1(ArrayList<String> images, Song song, Uri videoUri){ | |
context.startActivity(buildIntent(images,song,videoUri); | |
} | |
public Intent buildIntent(ArrayList<String> images, Song song, Uri videoUri){ | |
Intent intent = new Intent(ctx, SlideshowActivity.class); | |
intent.putExtra(EXTRA_IMAGE_FILE_NAMES, images); | |
intent.putExtra(EXTRA_SELECTED_SONG, song); | |
intent.putExtra(EXTRA_VIDEO_URI, videoUri); | |
return intent; | |
} | |
... (Othter Activies/Services/ etc | |
} | |
// factory with parameters: | |
public class InjectionFactory { | |
private Context context; | |
@Inject | |
public InjectionFactory(Context context){ | |
this.context = context; | |
} | |
//IntentFactoryParameters is an interface | |
public void start(IntentFactoryParameters parameters){ | |
Intent intent = buildIntent(parameters); | |
parameters.start(context, intent); | |
} | |
public Intent buildIntent(IntentFactoryParameters parameters){ | |
android.content.Intent intent = new android.content.Intent(context, parameters.getTargetContext()); | |
for (Map.Entry<String, Object> extraEntry : parameters.getExtraMap().entrySet()) { | |
intent.putExtra(extraEntry.getKey(), extraEntry.getValue()); | |
} | |
return intent; | |
} | |
} | |
public interface IntentFactoryParameters { | |
void start(Context context, Intent intent); | |
Class<? extends Context> getTargetContext(); | |
Map<String, Object> getExtraMap(); | |
} | |
//implementation | |
public class Activity1FactoryParameters implmemetns IntentFactoryParameters { | |
public void start(Context context, Intent intent){ | |
// same as above, essentially | |
} | |
public Class<? extends Context> getTargetContext(){ | |
return Activity1.class; | |
} | |
public Map<String, Object> getExtraMap(){ | |
//return built extra map | |
} | |
} | |
//usage: | |
@Inject | |
InjectionFactory injectionFactory; | |
... | |
injectionFactory.start(new Activity1FactoryParameters(images, song, videoUri)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment