Difference Between 2 Ways to Start an Activity?
I have seen the following two examples of starting activities in Android:
Example 1
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
Example 2
// Calling activity
NextActivity.开发者_如何学运维show(this)
// In the called activity
static void show(Context context) {
final Intent intent = new Intent(context, NextActivity.class);
context.startActivity(intent);
}
It seems the obvious difference between the two examples is that you attach the logic of how an activity is created to the implementation of the activity. Are there any other key differences? (e.g. is the calling activity told to wait until the called activity finishes in one case, but not in the other, etc.)
I see no difference to your 2 methods, other than the 2 lines of code in your first method just happen to be located in a static method that just happens to be located in the 2nd activity's class.
The actual lines of code that are being executed to start the activity are identical. Thus the behavior of the 2 methods will be identical.
Also, the code could be shortened to
context.startActivity(new Intent (context, NextActivity.class));
Only reason to create an instance of Intent as a field is if you need to set flags or add extras, etc.
精彩评论