Question: How can I launch a new Activity to a Fragment that is not the initial fragment? For example, the following code is wrong. I want to launch the MainActivity.class AT the SecondFragment.class. Seems simple enough but cannot find an answer anywhere. All help is greatly appreciated!

```
public void LaunchSecondFragment(View view) {
    view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.image_click));

    Intent intent = new Intent(this, SecondFragment.class);
    startActivity(intent);
}

```

Answer: 
So, before starting an activity you have to do something like:

```
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("launchSecondFragment", true)
startActivity(intent)

```

and in your MainActivity onCreate()

```
if(getIntent().getBooleanExtra("launchSecondFragment", false)) {
//do fragment transaction to second fragment
} else {
//do fragment transaction to the first fragment
}

```

**UPDATE**

So, here is the clever way to do it.

First of all create enum in your MainActivity.class

```
public enum FragmentNames {
FIRST_FRAGMENT,
SECOND_FRAGMENT
}

```

then define a string constant for getting and putting this extra(also in MainActivity)

```
public static final String FRAGMENT_EXTRA = "fragmentExtra";

```

So now when you start an activity you should do it like this:

```
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(MainActivity.FRAGMENT_EXTRA, MainActivity.FragmentNames.SECOND_FRAGMENT);
startActivity(intent);

```

And catch in your MainActivity onCreate() method:

```
FragmentNames name = getIntent().getSerializableExtra(FRAGMENT_EXTRA);
switch(name) {
case FIRST_FRAGMENT:
//do stuff
break;
case SECOND_FRAGMENT:
//do stuff
break;
default:
//load default fragment(FirstFragment for example)
}

```

What else is cool about enums? You mentioned that you are using this intents to define current item of your ViewPager. Well, good news, enums have ordinal().

Basically you can do something like:

```
mViewPager.setCurrentItem(name.ordinal());

```

In this case ordinal() of the FIRST\_FRAGMENT is 0 and ordinal of SECOND\_FRAGMENT is 1.

Just don't forget to check for nulls :)

Cheers.
When user clicks button and your `MainActivity` opens, its `onCreate()` will be get called. 

You should add fragment transaction in `onCreate()` to launch `SecondFragment` :

```
FragmentTransaction ft = getFragmentManager().beginTransaction();
SecondFragment secondFragment = new SecondFragment();
ft.replace(R.id.content_frame, secondFragment);
ft.commitAllowingStateLoss();

```