Ответ @ israel-dela-cruz правильный, вам нужно использовать extra
для различения флагов.Здесь более компактная версия, чтобы избежать использования магического числа и магического ключа :
public class RankingActivity extends Activity {
private static final ACTIVITY_OPTION_KEY = "activityOptionKey";
private static final int FROM_ACTIVITY_ONE = 1;
private static final int FROM_ACTIVITY_TWO = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
...
Bundle bundle = getIntent().getExtra();
int option = bundle.getInt(ACTIVITY_OPTION_KEY);
if(option == FROM_ACTIVITY_ONE) {
// do something when called from activity one
} else if(option == FROM_ACTIVITY_TWO) {
// do something when called from activity two
} else {
// is there something else?
}
...
}
// Use intent factory to remove dependency to magic number and magic key
public static Intent createIntentFromActivityOne(Activity activity) {
Intent intent = new Intent(activity, RatingActivity.class);
intent.putExtra(ACTIVITY_OPTION_KEY, FROM_ACTIVITY_ONE);
return intent;
}
public static Intent createIntentFromActivityTwo(Activity activity) {
Intent intent = new Intent(activity, RatingActivity.class);
intent.putExtra(ACTIVITY_OPTION_KEY, FROM_ACTIVITY_TWO);
return intent;
}
}
, тогда вы можете создать намерение, не зная магический ключ RatingActivity
имагическое число:
// from activity one
Intent intent = RatingActivity.createIntentFromActivityOne(this);
startActivity(intent);
// from activity two
Intent intent = RatingActivity.createIntentFromActivityTwo(this);
startActivity(intent);