Navigation drawer fragments overlap even with replace() - android

I have a navigation drawer in an activity. The activity's layout has a relative layout for a fragment. The id of relativelayout is mainContent in which I load the fragment(call it fragA) in the onCreate() of the activity. I have this same fragment also in the navigation drawer along with other navigation items. It is loaded oncreate of activity and can also be loaded from navigation drawer.
On selecting the navigation items, I replace the fragment in the mainContent which is the relativelayout in the activity with replace(). In the navigation drawer, the 0th item is the "fragB"(the same as fragA but in nav drawer). Second is another fragment(fragC). When I select fragB "more than once" and then select fragA and press back button, fragB and fragC overlap.
And also I have to keep pressing back button to go to initial screen because every time I select an item from nav drawer, new fragments fragBs and fragCs are created. Not replaced. I replace them in mainContent(first relative layout) in the activity's layout. I'm using FrameLayout for the fragments.
This is the activity's layout in which I place fragments:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.prematixsofs.taxiapp.DateVehiclePicker">
<!-- The main content view -->
<RelativeLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|bottom"></include>
</RelativeLayout>
<!-- The navigation drawer -->
<RelativeLayout
android:id="#+id/drawerPane"
android:layout_width="230dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<!-- Profile Box -->
<RelativeLayout
android:id="#+id/profileBox"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ff02c7c4"
android:padding="8dp">
<ImageView
android:id="#+id/avatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="15dp"
android:src="#drawable/user" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/avatar"
android:orientation="vertical">
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:textColor="#fff"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/viewProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginTop="4dp"
android:text="View Profile"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
<!-- List of Actions (pages) -->
<ListView
android:id="#+id/navList"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_below="#+id/profileBox"
android:background="#ffffffff"
android:choiceMode="singleChoice" />
<TextView
android:id="#+id/invisibleTextViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</RelativeLayout>
This is the Activity's code where I load the fragA:
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.mainContent, new DateTimePicker(),"tags").commit();
This is nav drawer onSelect:
private void selectItemFromDrawer(int position) {
Fragment fragment = null;
switch (position){
case 0: fragment = new DateTimePicker();
break;
case 1: fragment = new PreferencesFragment();
break;
case 2:
mDrawerLayout.closeDrawer(mDrawerPane);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(DateVehiclePicker.this);
alertDialog.setTitle("Logout");
// Setting Dialog Message
alertDialog.setMessage("Do you want to Logout?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
sessionManager.logoutUser();
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
dialog.cancel();
}
});
alertDialog.show();
}
if(fragment!=null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null).replace(R.id.mainContent, fragment);
fragmentTransaction.commit();
mDrawerLayout.closeDrawer(mDrawerPane);
}
This is where I set listener for nav drawer:
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItemFromDrawer(position);
}
});
I call the method selectItemFromDrawer(position);

You must handle BackStack for this you must clear all fragment which is on stack when you added or replace with main fragment
#Override
public void onBackPressed() {
Logg.e(TAG, getFragmentManager().getBackStackEntryCount());
try {
if (mNavigationDrawerFragment.isDrawerOpen())
{
mNavigationDrawerFragment.closeDrawer();
}
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
} else
{ Exit();
} catch (Exception e) {
e.printStackTrace();
}
}

Problem is you are creating new object for fragment transaction everytime you are trying to change fragment. Make it global in your code and use same instance everywhere.
Here is the code with switch case instead of if else
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment=new FragmentHome();
title="Home";
break;
case 1:
fragment=new FragmentAddress();
title="Update Address";
break;
case 2:
fragment=new FragmentHelpList();
title="My Helps";
break;
case 3:
fragment=new FragmentCompletedHelps();
title="History";
break;
case 4:
fragment=new FragmentSupport();
title="Account & Support";
break;
case 5:
fragment=new FragmentAbout();
title="About";
break;
case 6:
appSharedPreferences.setSession(false);
finish();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
Make the fragment variable global too

Try changing R.id.mainContent to R.id.container. I have similar setup but it is working fine with R.id.container.
fragmentManager.beginTransaction().replace(R.id.container,
new Registration_fragment()).commit();
and you have to replace FragA (not add) also initially during load.
fragmentTransaction.replace(R.id.mainContent, new DateTimePicker(),"tags").commit();

Related

Recylerview appends the old result on back pressed

I'm trying to create a application with bottom navigation view, But it looks like it is stacking up the fragments. Let me show you the picture to be clear.
[![Blank screen when back button is clicked again][3]][3]
Here is the code:
Home.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/bottomfrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:contentInsetStart="0dp"
app:fabAlignmentMode="center">
<com.rawat.soccermatch.CustomBottomNav
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemRippleColor="#android:color/transparent"
app:labelVisibilityMode="labeled"
app:itemIconTint="#color/enable_disable"
app:itemTextColor="#color/enable_disable"
app:menu="#menu/bottom_nav_menu"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF9800"
android:contentDescription="#string/title_create"
android:src="#drawable/ic_plusn"
app:tint="#fff"
app:layout_anchor="#id/bottom_app_bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Home.java
bt = findViewById(R.id.bottomNavigationView);
loadFragment(new HomeFrag()); // at first home fragment will be loaded
bt.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment mfragment;
int item_id=item.getItemId();
if(item_id==R.id.navigation_home){
mfragment = new HomeFrag();
loadFragment(mfragment);
}
else if(item_id==R.id.navigation_feed){
mfragment = new Feed();
loadFragment(mfragment);
}
else if(item_id==R.id.navigation_create){
mfragment = new creatematch();
loadFragment(mfragment);
}
else if (item_id==R.id.navigation_following){
mfragment = new Following();
loadFragment(mfragment);
}
else {
mfragment = new Account();
loadFragment(mfragment);
}
return true;
}
});
ft = findViewById(R.id.fab);
ft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ft.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
ft.setRippleColor(Color.GREEN);
Fragment fragment = new creatematch();
bt.getMenu().findItem(R.id.navigation_create).setChecked(true);
loadFragment(fragment);
}
});
}
private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.bottomfrag, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
Should I post HomFrag Code too?
I have edited the question now the problem which I understand is when clicking back my Home Fragment is resuming from where it was left but the recylerview values are resetting and appends the new results with old. Also the ripple color is not changing from following to home.
Try to use this function instead of yours
public void loadFragment(Fragment fragment) {
try {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.addToBackStack(null);
ft.replace(R.id.bottomfrag, fragment);
ft.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
you are adding that fragment to backstack so when you click back detach it, you need to make sure that your fragment is not added to backstack
Override onBackPressed() method. Inside it you can check what section is opened at the current moment and what you want to do in this case (nothing/close the app/navigate to another section).
in the bottom navigation activity you have to set default fragment is select which fragment you want for eg in common Home fragment always show as a default fragment so set homefragment as default.
set these code on create
bottomNavigationView.setSelectedItemId(R.id.nav_home);
checkon backpress
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
finish();
} else {
super.onBackPressed();
}
}

How to call a fragment inside a Fragment?

So I'm working in Fragments that has 2 Fragments in it. take a look at the image below:
The first fragment that shows the list of the inventory is named fragment_purchase_item_list and on the right side is the fragment_purchase_list
What I want to do is when I clicked SCAN from the toolbar on the right fragment, fragment_qrscan will replace fragment_purchase_item_list. Like this image below:
Can someone please see and check why my code is not working? Thanks!
PurchaseListFragment.java
<-- start of code snippet -->
#Override
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.btn_paypurchasetransaction:
Fragment chargeFragment = new ChargeFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.customersales_content, chargeFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
case R.id.layout_transaction_qrcode:
// Toast.makeText(getActivity(), "Scan Item", Toast.LENGTH_SHORT).show();
Fragment scanQRCodeFragment = new QRScanFragment();
FragmentManager fragmentManager1 = getFragmentManager();
FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
fragmentTransaction1.replace(R.id.inventory_fragment, scanQRCodeFragment);
fragmentTransaction1.addToBackStack(null);
fragmentTransaction1.commit();
break;
case R.id.layout_transaction_new:
Toast.makeText(getActivity(), "New Transaction", Toast.LENGTH_SHORT).show();
break;
}
}
<-- end of code snippet>
fragment_sales.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.SalesFragment"
android:orientation="horizontal"
android:id="#+id/customersales_content">
<fragment
android:id="#+id/inventory_fragment"
android:name="com.example.devcash.Fragments.PurchaseItemListFragment"
tools:layout="#layout/fragment_purchase_item_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3">
</fragment>
<View
style="#style/Divider"
android:layout_width="1dp"
android:layout_height="wrap_content" />
<fragment
android:id="#+id/purchaselist_fragment"
android:name="com.example.devcash.Fragments.PurchaseListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="#layout/fragment_purchase_list">
</fragment>
</LinearLayout>
Try replacing getFragmentManager() with getChildFragmentManager()

Fragment elements remain even when the fragment has been changed

I'm using a navigation drawer to switch fragments, but elements from the first fragment stay present even on changing fragments.
The code of the main activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
Drawer result = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.addDrawerItems(
item1,
new DividerDrawerItem(),
item2,
new SecondaryDrawerItem().withName(R.string.drawer_item_settings)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment fragment = new Fragment();
switch (position)
{
case 0:
fragment = new HomeFragment();
Log.d("Switch", "Search");
break;
case 1:
break;
case 2:
fragment = new InventoryFragment();
Log.d("Switch", "Inventory");
break;
case 3:
fragment = new SettingsFragment();
Log.d("Switch", "Settings");
break;
}
transaction.replace(R.id.main_frame, fragment);
transaction.commit();
return false;
}
})
.build();
}
The following is the code for activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="?android:windowBackground"
tools:context="com.example.nihal.xchange.MainActivity">
...
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/main_frame">
<fragment
android:name="com.example.nihal.xchange.HomeFragment"
android:id="#+id/current_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Elements from Home Fragment remain present across fragments but this behavior is not exhibited by elements of other fragment.
How do I prevent this from happening?
Don't include the Home Fragment in the xml. Keep your container FrameLayout empty and add the initial fragment as soon as the activity starts. In your Activity's onCreate method, add the Home fragment dynamically in the beginning.
activity_main.xml -
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/main_frame">
<!-- Removed the fragment from here -->
</FrameLayout>
In the onCreate(), add it dynamically -
if (findViewById(R.id.main_frame) != null) {
if (savedInstanceState != null)
return;
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.main_frame, new HomeFragment());
transaction.commit();
}

change class in fragment throw activity

i have three fragments, and one activity.
i have put the two fragments side by side in the activity and i would like when click on the first fragment, to change it to the third one.
activity :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:id="#+id/parent">
<fragment
android:id="#+id/mainPage"
android:layout_width="0dp"
android:layout_weight="0.7"
android:layout_height="wrap_content"
class="com.example.user1.lotus.MainMenu" >
</fragment>
<fragment
android:id="#+id/side_bar"
android:layout_width="0dp"
android:layout_weight="0.3"
android:layout_height="wrap_content"
class="com.example.user1.lotus.SideBar" >
</fragment>
</LinearLayout>
now i would like to change class="com.example.user1.lotus.MainMenu"
to class="com.example.user1.lotus.Starter" through my activity
Is this possible?
in activity
public void onFragmentInteraction(String s){
switch (s){
case "menu":
break;
case "starters":
break;
case "mainDish":
MainDish newFragment = new MainDish();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.mainPage, newFragment);
transaction.addToBackStack(null);
transaction.commit();
and in the fragment
btnMainDish.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
mListener.onFragmentInteraction("mainDish");
}
});
Just do this in the onClick listener method of the button your fragment will get replaced by the third fragment:
ThirdFragment newFragment = new ThirdFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.layout_replace, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Hope this helps.

Replacing Fragment : Didn't work

I have a Button in Fragment. If Button is clicked it has to replace the Fragment with another Fragment. The Fragments in activity_main.xml is created statically.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efefef"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/bHome"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#000000"
android:padding="3dp"
android:text="Home"
android:textColor="#ffffff" />
<Button
android:id="#+id/bEvents"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#f8bd0e"
android:padding="3dp"
android:text="Events" />
<Button
android:id="#+id/bNearby"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#f8bd0e"
android:padding="3dp"
android:text="Nearby" />
</LinearLayout>
<LinearLayout
android:id="#+id/llFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<fragment
android:id="#+id/fHome"
android:name="com.example.fragmentstack.Home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
<fragment
android:id="#+id/fEvents"
android:name="com.example.fragmentstack.Events"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
<fragment
android:id="#+id/fNear"
android:name="com.example.fragmentstack.NearBy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
</LinearLayout>
The output of activity_main.xml is
Main Activity:
public class MainActivity extends FragmentActivity implements OnClickListener {
Button home, events, near;
Fragment f1, f2, f3, f4;
FragmentManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
home = (Button) findViewById(R.id.bHome);
events = (Button) findViewById(R.id.bEvents);
near = (Button) findViewById(R.id.bNearby);
home.setOnClickListener(this);
events.setOnClickListener(this);
near.setOnClickListener(this);
manager = getSupportFragmentManager();
f1 = manager.findFragmentById(R.id.fHome);
f2 = manager.findFragmentById(R.id.fEvents);
f3 = manager.findFragmentById(R.id.fNear);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bHome:
if (home.isPressed()) {
home.setBackgroundColor(Color.BLACK);
home.setTextColor(Color.WHITE);
events.setBackgroundResource(R.color.Yellow);
near.setBackgroundResource(R.color.Yellow);
events.setTextColor(Color.BLACK);
near.setTextColor(Color.BLACK);
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.hide(f2);
transaction.hide(f3);
transaction.show(f1);
transaction.commit();
}
break;
case R.id.bEvents:
if (events.isPressed()) {
events.setBackgroundColor(Color.BLACK);
events.setTextColor(Color.WHITE);
home.setBackgroundResource(R.color.Yellow);
near.setBackgroundResource(R.color.Yellow);
home.setTextColor(Color.BLACK);
near.setTextColor(Color.BLACK);
FragmentTransaction transaction1 = getSupportFragmentManager()
.beginTransaction();
transaction1.hide(f1);
transaction1.hide(f3);
transaction1.show(f2);
transaction1.commit();
}
break;
case R.id.bNearby:
if (near.isPressed()) {
near.setBackgroundColor(Color.BLACK);
near.setTextColor(Color.WHITE);
home.setBackgroundResource(R.color.Yellow);
events.setBackgroundResource(R.color.Yellow);
home.setTextColor(Color.BLACK);
events.setTextColor(Color.BLACK);
FragmentTransaction transaction2 = getSupportFragmentManager()
.beginTransaction();
transaction2.hide(f1);
transaction2.hide(f2);
transaction2.show(f3);
transaction2.commit();
}
break;
}
}
}
Now Home extends Fragment. the xml layout of home has one Button and one TextView.Here onClicking Button I'm replacing the Home Fragment with new Update Profile Fragment.
public class Home extends Fragment {
Button btn;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.home, container, false);
init();
return view;
}
private void init() {
btn = (Button) view.findViewById(R.id.bUpdate);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fg = new UpdateProfile();
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fg);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
R.id.fragment_container is present in activity_main.xml. Now the problem I'm facing is when clicking the button it is not replacing the fragment with new Fragment.
Now the problem I'm facing is when clicking the button it is not
replacing the fragment with new Fragment.
The transaction does occur but it's not visible as you add that fragment to a container that will be pushed out of the screen(all of your layout's fragments have width set to match_parent so they will fill the LinearLayout's width and the FrameLayout will be pushed out of the screen).
Also you do the replace transaction on a container that doesn't hold the previous fragments, this means that the old fragments will still be visible on the screen(plus the new added fragment).
The system you setup is not the proper way to handle your scenario. From the images it seems you have a tab like layout and in this case you should have only one container in which all of your fragments will reside(to avoid the extra work of showing/hiding the proper fragment and also provide a decent BACK button experience).
Secondly if you're going to work(do transactions) with those fragments you should really add them programmatically as replace will not go well with a static fragments. There are a lot of tutorials out there on how to make a tab like layout with fragments.

Categories

Resources