简体   繁体   English

我如何向不是初始片段的片段启动新的活动?

[英]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. 我想在SecondFragment.class上启动MainActivity.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() 并在您的MainActivity onCreate()中

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

UPDATE UPDATE

So, here is the clever way to do it. 因此,这是一种聪明的方法。

First of all create enum in your MainActivity.class 首先在MainActivity.class中创建枚举

public enum FragmentNames {
FIRST_FRAGMENT,
SECOND_FRAGMENT
}

then define a string constant for getting and putting this extra(also in MainActivity) 然后定义一个字符串常量以获取并放置此多余的字符串(也在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: 并捕获您的MainActivity onCreate()方法:

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. 您提到您正在使用此意图来定义ViewPager的当前项目。 Well, good news, enums have ordinal(). 好吧,好消息,枚举具有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. 在这种情况下,FIRST_FRAGMENT的ordinal()为0,SECOND_FRAGMENT的ordinal为1。

Just don't forget to check for nulls :) 只是不要忘记检查null :)

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 这是针对MainActivity的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. 当用户单击按钮并打开您的MainActivity ,将调用其onCreate()

You should add fragment transaction in onCreate() to launch SecondFragment : 您应该在onCreate()添加片段事务以启动SecondFragment

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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