Last Updated: February 25, 2016
·
4.559K
· dadino

Android - Open activities with a static method

A type-safe way to open activities in Android

public static void show(Context context, String strParam, int intParam) {
    Intent intent = new Intent(context, ActivityToOpen.class);
    intent.putExtra(STRING_PARAM, strParam);
    intent.putExtra(INT_PARAM, intParam);
        intent.addFlags(YOUR_INTENT_FLAGS);
    context.startActivity(intent);
}

Then read those parameters from the onCreate method of the activity you want to open

...
Bundle args = getIntent().getExtras();
if(args != null){
    mInt= args.getInt(INT_PARAM);
    mString= args.getString(STRING_PARAM);
  }
...

You can call

ActivityToOpen.show(context, string, int)

from anywhere and it will show your activity.