I want to create a method to start multiple activities. I have set setOnClickListener
on every button. I have implemented onClick()
method that looks like this:
public void onClick(View view) {
switch (view.getId()) {
case R.id.firstActivityButton:
Intent i1 = new Intent(getApplicationContext(), FirstActivity.class);
startActivity(i1);
break;
case R.id.secondActivityButton:
Intent i2 = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(i2);
break;
//and so on
}
}
I want to use a method in every case
like this: startSpecificActivity(FirstActivity.class)
This is my method:
public void startSpecificActivity(Context context) {
Intent intent = new Intent(getApplicationContext(), context.class);
startActivity(intent);
}
Create method like this where
Class<?>
is a generic class object holder and?
is a wildcard character:And I also encourage to use the
context
orYourActivityName.this
of current Activity, instead ofgetApplicationContext()
.