user3727505
user3727505

Reputation: 1

Android: Fragment

When frButton is clicked FragmentOne should open and MainActivity should be hidden but Components of MainActivity is still accessible in FragmentOne, which is unwanted. How to fix it?

code for MainActivity:

public void gofag1(View v) {
    // TODO Auto-generated method stub
    FragmentOne frag = new FragmentOne();
    FragmentManager fm = getFragmentManager(); // import
    FragmentTransaction ftr = fm.beginTransaction(); // import
    ftr.add(R.id.mainlayout, frag, "keyFrag");
    ftr.addToBackStack(null);
    ftr.commit();
}

code for FragmentOne:

public class FragmentOne extends Fragment {

public FragmentOne() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_one, container,
            false);
    return v;
}
}

code for FragmentOne layout... R.layout.fragment_one

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.megavb.le.FragmentOne" >

<!-- TODO: Update blank fragment layout -->

<TextView
    android:id="@+id/tOnOff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="22dp"
    android:text="@string/a1" />

<ToggleButton
    android:id="@+id/toggleButton1"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:onClick="reedToggleValue"
    android:text="ToggleButton"
    android:textSize="30sp" />

code for MainActivity layout.... R.id.mainlayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical" >


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:onClick="gofag1"
    android:text="fOne" />

</RelativeLayout>   

Please see the picture for better understanding...

PICTURE HERE

Upvotes: 0

Views: 96

Answers (1)

Sean C.
Sean C.

Reputation: 228

So, you should be using ftr.replace instead of ftr.add

public void gofag1(View v) {
    // TODO Auto-generated method stub
    FragmentOne frag = new FragmentOne();
    FragmentManager fm = getFragmentManager(); // import
    FragmentTransaction ftr = fm.beginTransaction(); // import

    //Notice the change here
    ftr.replace(R.id.mainlayout, frag, "keyFrag");
    ftr.addToBackStack(null);
    ftr.commit();
}

Hope this helps!

EDIT: Well, without more information I'm going to take another stab at this.

In your mainlayout.xml you need to define a Fragment Container a simple method of doing this would be to use a layout file like the one that follows as a base for your Activity.

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container">
</FrameLayout>

after defining this new fragment container you can programmatically add fragments to it by referencing it's ID.

The next step is to move your original MainActivty code into a new fragment!

This is the code for fragment_main

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:onClick="gofag1"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Here we create a new layout that simply contains a button.

Next we create the actual fragment

public class FragmentMain extends Fragment {

    public FragmentMain() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_main, container,
                false);
}

From here we can change our Main activity OnCreate code to

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainlayout);
    FragmentMain frag = new FragmentMain();
    FragmentManager fm = getFragmentManager(); // import
    FragmentTransaction ftr = fm.beginTransaction(); // import
    ftr.add(R.id.fragment_container, frag, "keyFrag");
    ftr.addToBackStack(null);
    ftr.commit();


}

Upvotes: 3

Related Questions