开发者

how to hide frag and refresh the activity to show this

i have a button called ac and when i click on it, i want to hide my two fragment called a and b, but this is not happening, what do I need to do to make this work?

package开发者_高级运维 cmsc436.lab5;


import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 import android.widget.LinearLayout;

  public class Lab5Activity extends Activity  implements button2interface, button1interface{
/** Called when the activity is first created. */
button1 a;
button2 b ;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;

@Override
public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
        a= new button1();
        b=new button2();
     final LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setId(1);
     fragmentManager = getFragmentManager();
    fragmentTransaction = fragmentManager.beginTransaction();

    //a.getActivity().findViewById(1);
    fragmentTransaction.add(linearLayout.getId(), a);
    fragmentTransaction.add(linearLayout.getId(), b);
    fragmentTransaction.commit();

    final Button ac =new Button(this);
    ac.setText("c button");

    linearLayout.addView(ac);
    setContentView(linearLayout);


    ac.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            fragmentTransaction.hide(a);
            fragmentTransaction.hide(b);

        }
    });

}

@Override
public void buttonClick1() {
    if (b.isHidden()) {
        fragmentTransaction.show(b);
        }
        else {
        fragmentTransaction.hide(b);
        }   
}

@Override
public void buttonClick2() {
    if (a.isHidden()) {
        fragmentTransaction.show(a);
        }
        else {
        fragmentTransaction.hide(a);
        }   
}

}


I haven't really worked with fragments before, but it looks like you're trying to re-use a FragmentTransaction that's already been committed. You don't want a member variable for your FragmentTransaction; you should create a new one every time you need it:

public void onClick(View v) {
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.hide(a);
    fragmentTransaction.hide(b);
    fragmentTransaction.commit();
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜