Static method for activity launching

There are three benefits of using static activity launchers:

a) If you’re in a team and you’re working on a new activity, which will be then used by your coworkers, they’ll get a predefined entry point(s) to that activity. They won’t have to scan through your onCreate()‘s code to check what kind of parameters it needs to work correctly and what are the keys for intent’s extras.

b) No boilerplate. Build the intent with its extras in one place and reuse that code later.

c) You get a type-safe invocation and parameters check at compile time, rather than unexpected deserializing crashes from your Parcelable instances in onCreate().

Sample activity:

public class ArticleActivity extends BaseActivity {

    public static void launch(Article article, Context context) {
        Intent launchIntent = new Intent(context, ArticleActivity.class);
        launchIntent.putExtra(Constants.EXTRA_ARTICLE, article);
        context.startActivity(launchIntent);
    }

    public static void launch(Article article, boolean inFullScreen, Context context) {
        Intent launchIntent = new Intent(context, ArticleActivity.class);
        launchIntent.putExtra(Constants.EXTRA_ARTICLE, article);
        launchIntent.putExtra(Constants.EXTRA_IN_FULL_SCREEN, inFullScreen);
        context.startActivity(launchIntent);
    }

    //...

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

        Bundle extras = getIntent().getExtras();
        //...
    }
}

Of course, the Article class must implement the Parcelable interface. Launching from other activity:

ArticleActivity.launch(article, false, this);

You can also apply the same pattern to services.