A tidier way than the accepted answer would be to use Serializable
or Parcelable
.
Here is an example of how to do it using Serializable
:
In your first activity...
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("EXTRA_NEXT_ACTIVITY_CLASS", ThirdActivity.class);
startActivity(intent);
Then in your second activity...
Bundle extras = getIntent().getExtras();
Class nextActivityClass = (Class<Activity>)extras.getSerializable("EXTRA_NEXT_ACTIVITY_CLASS");
Intent intent = new Intent(SecondActivity.this, nextActivityClass);
startActivity(intent);
Doing it with Parcelable
is pretty much the same, except you would replace extras.getSerializable("EXTRA_NEXT_ACTIVITY_CLASS")
in the above code with extras.getParcelable("EXTRA_NEXT_ACTIVITY_CLASS")
.
The Parcelable method will be faster, but harder to set up (as you need to make your third Activity implement Parcelable
- see http://developer.android.com/reference/android/os/Parcelable.html).