Reputation: 43
I've run into an issue with my code.
What I want
My Activity starts automatically with Fragment01. On Fragment01, there is a button, which should replace Fragment01 with Fragment02
What happens
When I press the button, instead of replacing Fragment01, I can still see Fragment01 in the background
MainActivity.java:
package com.lucky.fragmentexample;
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;
public class MainActivity extends Activity {
Button bn;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn = (Button)findViewById(R.id.button1);
bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Fragment02 fragment02 = new Fragment02();
fragmentTransaction.replace(R.id.button_fragment_container, fragment02);
fragmentTransaction.commit();
}
});
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/button_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/fragment01"
android:name="com.lucky.fragmentexample.Fragment01"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
Upvotes: 1
Views: 230
Reputation: 13012
Your fragment
element in your layout XML can only ever refer to that single fragment and can't be swapped out in the way you intend. Instead, you need to remove the fragment
element from your layout XML entirely and add your Fragment01
into your container FrameLayout
in code in the onCreate()
method of your activity.
So, remove the fragment
element, like so;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/button_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Then you can perform your transaction in the beginning of your onCreate()
override, like so;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
transaction.replace(R.id.button_fragment_container, new Fragment01());
transaction.commit();
You will want to instantiate a new FragmentTransaction
for each new transaction, which means you'll probably want to remove the FragmentManager
and FragmentTransaction
from your instance members and declare them fresh each time you need to make a fragment transaction.
I hope this helps.
Further reading:
Upvotes: 1
Reputation: 604
Just go step by step:
First modify your layout, remove the fragment over there and make the reference of your FrameLayout in the activity.
Now initiate your fragment class which you want to add to the framelayout and call following code:
FragmentTransaction transaction = manager.beginTransaction();
transaction.remove(removePreviousFragmentIfAny);
transaction.add(R.id.button_fragment_container, newFragment);
transaction.commit();
Hope it will help you to go through.
Upvotes: 1
Reputation: 6034
try this code :
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment01, new Fragment02());
fragmentTransaction.commit();
also, change the 'fragment' to 'FrameLayout' in your xml. This prevents from any one fragment always remaining in the background.
Upvotes: 1
Reputation: 3972
Put beginTransaction inside onclick function and replace R.id.button_fragment_container for R.id.frame_container.
Use this inside onclick:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, new Fragment02()).commit();
Fragment inside xml file is not correct. Erase that part.
Upvotes: 1