snippetjavaMinor
Static method to create intents
Viewed 0 times
intentscreatestaticmethod
Problem
I had the idea to have a static method on an activity which creates intents that it can use. For example if I have an activity with a webview that can load html from a resource or a URL I would have 2 methods in the activity like so.
The on create would look like this..
Then a class that wants to open that activity would use it in this way:
Is there any problems with using this approach?
public static Intent createURLIntent(Context context, int titleId, String URL) {
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra(TITLE_ID, titleId);
intent.putExtra(URL, URL);
return intent;
}
public static Intent createFileIntent(Context context, int titleId, String fileName) {
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra(TITLE_ID, titleId);
intent.putExtra(FILE, fileName);
return intent;
}The on create would look like this..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_activity);
int titleId = getIntent().getIntExtra(TITLE_ID, 0);
setTitle(getString(titleId));
if (getIntent().hasExtra(FILE)) {
webview.loadUrl(getIntent().getStringExtra(FILE));
} else {
webview.loadUrl(getIntent().getStringExtra(URL));
}
}Then a class that wants to open that activity would use it in this way:
Intent intent = WebViewActivity.createURLIntent(
context, R.string.about_title, "www.google.com");
startActivity(intent);Is there any problems with using this approach?
Solution
Judging by this bit:
It looks like the
If you really want to have two separate static method for some reason, then extract the common logic in these methods to a helper method.
if (getIntent().hasExtra(FILE)) {
webview.loadUrl(getIntent().getStringExtra(FILE));
} else {
webview.loadUrl(getIntent().getStringExtra(URL));
}It looks like the
loadUrl method doesn't care whether the parameter you pass to it is a file path out a web address. Actually the term "url" has a general meaning that can encompass both file paths and web addresses. So I don't see the point of distinguishing these two in the intents: just use "URL", it doesn't matter if it's a path out web address. Again, this suggestion is based on the posted code.If you really want to have two separate static method for some reason, then extract the common logic in these methods to a helper method.
private static Intent createIntent(Context context, int titleId, String key, String url) {
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra(TITLE_ID, titleId);
intent.putExtra(key, url);
return intent;
}
public static Intent createURLIntent(Context context, int titleId, String url) {
return createIntent(context, titleId, URL, url);
}
public static Intent createFileIntent(Context context, int titleId, String path {
return createIntent(context, titleId, FILE, path);
}Code Snippets
if (getIntent().hasExtra(FILE)) {
webview.loadUrl(getIntent().getStringExtra(FILE));
} else {
webview.loadUrl(getIntent().getStringExtra(URL));
}private static Intent createIntent(Context context, int titleId, String key, String url) {
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra(TITLE_ID, titleId);
intent.putExtra(key, url);
return intent;
}
public static Intent createURLIntent(Context context, int titleId, String url) {
return createIntent(context, titleId, URL, url);
}
public static Intent createFileIntent(Context context, int titleId, String path {
return createIntent(context, titleId, FILE, path);
}Context
StackExchange Code Review Q#101343, answer score: 3
Revisions (0)
No revisions yet.