0

如何启动一个新的活动到不是最初片段的片段?例如,下面的代码是错误的。我想在SecondFragment.class中启动MainActivity.class。似乎很简单,但无法在任何地方找到答案。所有帮助非常感谢!如何向不是初始片段的片段启动新的活动?

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

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

所以要启动MainActivity _from_ SecondFragment,对不对?因为你的代码看起来像你想要启动SecondFragment,如果是这种情况,那么你应该知道碎片通常不是通过意图启动的,你必须改用FragmentManager。 – Kistamushken

+0

好吧,我的MainActivity不是用户看到的第一个活动。我们来调用这个初始活动FeedActivity。用户点击一个按钮,将其启动到MainActivity(FirstFragment)中,但我希望它直接启动到SecondFragment。那有意义吗? – drearypanoramic

+1

加油吧。让它更多一点英语...... :( –

回答

2

所以,开始你必须做的是这样一个活动之前:

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

,并在您的MainActivity的onCreate()

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

UPDATE

所以,这里是聪明的做法。

首先在MainActivity.class创建枚举

public enum FragmentNames { 
FIRST_FRAGMENT, 
SECOND_FRAGMENT 
} 

然后定义字符串常量用于获取并把这个额外的(也是在MainActivity)

public static final String FRAGMENT_EXTRA = "fragmentExtra"; 

所以,现在当您启动活动你应该做这样的:

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

,赶上在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) 
} 

还有什么关于枚举的很酷吗?你提到你正在使用这个意图来定义ViewPager的当前项目。好吧,好消息,枚举有序()。

基本上,你可以这样做:

mViewPager.setCurrentItem(name.ordinal()); 

在这种情况下序()的FIRST_FRAGMENT是0和SECOND_FRAGMENT的序号为1

只是不要忘了检查空值: )

干杯。

+0

这个答案工作得很好,很容易实现。我只是将我的mViewPager中的currentItem设置为我想要访问的片段的编号,即mViewPager.setCurrentItem(1);其中1是“第二”片段,0是“第一”。我把这一行放在您的评论“//将事务片段化到第二个片段”所在的位置。谢谢! – drearypanoramic

+1

不客气。如果你正在处理不复杂的逻辑,这种模式是可以的。如果你想实现更简洁的东西,你最好使用像@Niko Yowono建议的开关盒。我会稍微更新一下我的答案,告诉你,我该怎么做。 – Kistamushken

+0

真棒,谢谢!这很有意义,并且清除了我不知道的碎片的许多概念。附:你认为你可以对我的问题进行投票吗? – drearypanoramic

1

当用户点击按钮,您的MainActivity打开时,其onCreate()将被调用。

你应该onCreate()添加片段交易推出SecondFragment

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

试试这个启动活动:

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

这对于在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; 
}