问题描述
我想实现,有一个片段的活动!当我点击片段1,Fragment2被调用,当我点击Fragment2,Fragment2应该从屏幕上消失!我实现它通过调用Fragment2中的LinearLayout的setOnclickListener在其onCreateView,和我onclicklistener,我叫
transaction.remove(myFragment);
器transaction.commit();
但在那之后我遇到这个错误:提交已经被称为我该如何解决这个错误,这里是我的code:这是我的片段类
公共类ArticleFragment扩展片段{
最终静态字符串ARG_POSITION =位置;
INT mCurrentPosition = -1;
私有静态的LinearLayout升;
私人android.support.v4.app.FragmentTransaction交易;
私人片段newFragment;
公共无效setArticleFragment(android.support.v4.app.FragmentTransaction交易,片段newFragment){
this.transaction =交易;
this.newFragment = newFragment;
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
如果(savedInstanceState!= NULL){
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
视图V = inflater.inflate(R.layout.article_view,集装箱,假);
L =(的LinearLayout)v.findViewById(R.id.transparentArea);
如果(L == NULL)
l.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
transaction.remove(newFragment);
器transaction.commit();
}
});
返回伏;
}
}
和我mainAcitivity类
公共类MainActivity扩展FragmentActivity工具
HeadlinesFragment.OnHeadlineSelectedListener {
HeadlinesFragment firstFragment;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.news_articles);
如果(findViewById(R.id.fragment_container)!= NULL){
如果(savedInstanceState!= NULL){
返回;
}
firstFragment =新HeadlinesFragment();
firstFragment.setArguments(getIntent()getExtras());
getSupportFragmentManager()的BeginTransaction()
。新增(R.id.fragment_container,firstFragment).commit();
}
}
公共无效onArticleSelected(INT位置){
ArticleFragment articleFrag =(ArticleFragment)getSupportFragmentManager()
.findFragmentById(R.id.article_fragment);
如果(articleFrag!= NULL){
articleFrag.updateArticleView(位置);
} 其他 {
最后ArticleFragment newFragment =新ArticleFragment();
捆绑的args =新包();
args.putInt(ArticleFragment.ARG_POSITION,位置);
newFragment.setArguments(参数);
最后FragmentTransaction交易= getSupportFragmentManager()
.beginTransaction();
newFragment.setArticleFragment(事务,newFragment);
transaction.setCustomAnimations(R.anim.slidein,R.anim.slideout);
transaction.add(R.id.fragment_container,newFragment);
器transaction.commit();
}
}
而不是重用传入的事务,创建一个新的 FragmentTransaction
实例,消除Fragment2。
甚至更容易是添加的第一个片段事务片段回栈(如 addToBackStack(空)
),然后在Fragment2,刚刚流行的背部栈 FragmentManager
popBackStack()
。
I want to implement an activity that has a fragment! when I click on Fragment1 , Fragment2 is called , and when I click on Fragment2 , Fragment2 should be removed from the screen!I implement it by calling setOnclickListener of LinearLayout of Fragment2 in its onCreateView , and on my onclicklistener , I called
transaction.remove(myFragment);
transaction.commit();
but after that I faced to this error : commit already calledhow can I fix this error , here is my code:It is my fragment class
public class ArticleFragment extends Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
private static LinearLayout l;
private android.support.v4.app.FragmentTransaction transaction;
private Fragment newFragment;
public void setArticleFragment(android.support.v4.app.FragmentTransaction transaction , Fragment newFragment) {
this.transaction = transaction;
this.newFragment = newFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
View v = inflater.inflate(R.layout.article_view, container , false);
l = (LinearLayout) v.findViewById(R.id.transparentArea);
if(l == null)
l.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
transaction.remove(newFragment);
transaction.commit();
}
});
return v;
}
}
and my mainAcitivity class
public class MainActivity extends FragmentActivity implements
HeadlinesFragment.OnHeadlineSelectedListener{
HeadlinesFragment firstFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
firstFragment = new HeadlinesFragment();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
public void onArticleSelected(int position) {
ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager()
.findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
articleFrag.updateArticleView(position);
} else {
final ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
final FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
newFragment.setArticleFragment(transaction, newFragment);
transaction.setCustomAnimations(R.anim.slidein, R.anim.slideout);
transaction.add(R.id.fragment_container, newFragment);
transaction.commit();
}
}
Instead of reusing the passed-in transaction, create a new FragmentTransaction
instance that removes Fragment2.
Even easier is to add the first fragment transaction to fragment back stack (e.g. addToBackStack(null)
) and then in Fragment2, just pop the back stack with FragmentManager
popBackStack()
.
这篇关于安卓埃罗:"承诺已经称为QUOT;当删除片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!