2013-11-27 24 views
2

我是一个新的Android开发人员,所以我更喜欢问正确的方式,因此解决我的问题。正确的方式来推动视图与行动吧

我有一个FragmentActivity和多个片段,我的片段之一是按钮组成应该显示新的视图像在iOS上的导航控制器的推视图。

我试图创建新的活动并使用Intent显示它们,但actionBar被剥开了。

那么我想创建一个与actionBar一起工作并保留我的操作栏?

预先感谢您。

回答

5

我会回答自己,因为我找到解决方案,也许会有所帮助。 我会比较iOS。

如果您使用的是拥有片段的actionBar,并且想要打开新视图,则有两种不同的方法。

如果您想在actionBar和其他所有元素上添加新视图,请创建一个新的Activity并使用Intent。 这种方法就像在iOS

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class); 
myIntent.putExtra("key", value); //Optional parameters 
CurrentActivity.this.startActivity(myIntent); 

模式的看法。如果你想在动作条中增加一个新的观点,创建一个新的片段,并使用此代码:

Fragment newFragment = new MyNewFragmentClass(); 
    FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

    // Replace whatever is in the fragment_container view with this fragment, 
    // and add the transaction to the back stack 
    transaction.replace(R.id.my_current_layout, newFragment); 
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
    transaction.addToBackStack(null); 

    // Commit the transaction 
    transaction.commit();