Happy New Years, Simple pattern for opening an Android Activity
Another New Year and like many other bloggers I’m thinking about resolutions of writing on my blog more often. Actually, my resolutions are a little grander then just this blog, but I’m still ironing them out.
So instead of going into my life/work goals of 2011 I’d rather share a code snippet/Android pattern I’ve been using quite a bit lately. It grew from working with other Android developers and wanting to specify a mini API for opening an Activity. Many Activities pass data when they open another Android Activity; using Intents, name/value pairs can be defined for the data elements. One problem is though, you must use code inspection to determine what data is passed in, and what the format it is. What I’ve started to do is use a static method in the Activity as a means of starting it. Here’s a simple code snippet to demonstrate what I’m talking about
public class ActivityOne extends Activity
{
private static final String VALUE1 = "value1_param";
private static final String VALUE2 = "value2_param";
public static void startMe( Context ctx, int val1, String val2 )
{
Intent intent = new Intent( ctx, ActivityOne.class );
intent.putExtra( VALUE1, val1 );
intent.putExtra( VALUE2, val2 );
ctx.startActivity( intent );
}
private int _value1;
private String _value2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_one );
Bundle extras = getIntent().getExtras();
if ( extras != null )
{
_value1 = extras.getInt( VALUE1 );
_value2 = extras.getString( VALUE2 );
}
}
}
This way ActivityOne can be started from another Android Activity using the following call,
ActivityOne.startMe( this, 123, "abc" );
This places the actual means of defining the Intent for ActivityOne in one place, and if this contract is changed, ie adding another variable or changing types, it can be changed in the method signature of startMe and all the places this is called will fall out using a coding tool like Eclipse.
Hope this little snippet can give you some ways on writing cleaner Android code, Happy New Years!

January 1st, 2011 at 9:22 pm
If you’re already accessing the class in a static way, why not just set val1 and val2 at the same time?
I think this would increase performance and memory and save yourself a line or two of code.
January 1st, 2011 at 11:23 pm
Actually, I messed up there, in my haste to publish I forgot one of the most important lines (just updated), the line startActivity creates the instance of ActivityOne then onCreate is called. I don’t want to the values _value1 and _value2 to be static because I want them to be set for each instance of ActivityOne.
January 2nd, 2011 at 6:37 am
Your “simple pattern” is only useful for an Activity that you don’t share with other applications.
The main goal of the Activity is beeing created by everyone and not only by your application.
So, it’s important to specify that this pattern is only useful for the developer of THIS app.
January 2nd, 2011 at 10:47 am
Good point Davy, the project I’ve started using this on is with a number of different devs and a large number of screens/activities, so yes it is meant for calls within the same Android App.
January 3rd, 2011 at 5:35 pm
It’s not true to say it’s only useful for an Activity that’s not shared. There’s no reason why it this pattern cannot be used anyway. Using this pattern makes no difference to outside users of the app — they just have to use the normal Intent mechanism rather than this static method.
This is just a convenience for when the Activity *is* launched from within the same app.
January 4th, 2011 at 3:25 am
[…] posted a great article titled: Simple pattern for opening an Android […]
January 7th, 2011 at 7:12 pm
Thanks - I like your idea a lot. Will refactor my code to try it out.