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.
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?