in Android Code Tips

Simple pattern for opening an Android Activity

michael posted a great article titled: Simple pattern for opening an Android Activity.

You should definitely read it, I loved it!

Here is the code snippet:

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 );
        }
    }
}
ActivityOne.startMe( this, 123, "abc" );
Share