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.
Written by David Corsalini
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Android
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#