2014-01-20 68 views

回答

0

你最好打的是FragmentTransaction.replace()

例如为:

// Create new fragment and transaction 
Fragment newFragment = new ExampleFragment(); 
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.fragment_container, newFragment); 
transaction.addToBackStack(null); 

// Commit the transaction 
transaction.commit(); 

但是,请记住,你只能添加一个片段类的一个实例一次。试图再次添加相同的实例将导致Fragment already added异常。要解决此问题,可以使用MyFragmentClass.instantiate()new MyFragmentClass()获取您需要添加的片段类的新实例。

+0

我已经使用它重新载入以前的fragment.I想重新刷新片段..... – Vishwajeet

+0

这是不可能的,除非你在片段的构造函数或'onCreate/onResume()'中使用某种数据恢复。实质上,新的MyFragmentClass()将始终返回该类的新实例。您应该在答案中包含一些代码,以便我们更好地了解您的问题。 –

相关问题