简体   繁体   中英

How can I launch a new Activity to a Fragment that is not the initial fragment?

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);
}

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.

Try this to start the activity:

Intent intent = new Intent(this, MainActivity.class);   
int fragmentIndex = 2;
intent.putExtra("fragment_index", fragmentIndex);
startActivity(intent);

and this for the MainActivity's onCreate

Bundle extras = getIntent().getExtras();
int fragmentIndex;
if(extras != null) {
    fragmentIndex = extras.getInt("fragment_index",1);
}
switch(fragmentIndex) {
    case 1:
        //display fragment 1
        break;
    case 2:
        //display fragment 2
        break;
    case 3:
        //display fragment 3
        break;
}

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM