android - Fragment.replace doesn't remove one fragment, works with multiple others -
edit: problem below occurs on samsung galaxy s3, however, when run same app on sony xperia z3+ doesn't display wifi list @ all. :-/
i have weird situation app. have 5 different fragments. of them work expected when doing fragmenttransaction
s except one.
initially, when started app used 1 of android studio templates, seemed serious overkill used fragments
instead of listview
listing wifi items. it's been while since i've developed android, i'm playing catch-up.
i left code in place , carried on developing interface. trouble came along when decided remove code populated main container fragment
"items" , replace fragment
containing listview
.
all fragments
work expected , replaced expected when select new item menu except new listview fragment
, remains in background once select it.
after select it, if select other fragments
change should, first 1 stays in place.
the closest question i've found situation this one, didn't me.
this screen looks after selecting problem fragment
, fragment
:
my mainactivity
oncreate()
method:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); setupwifiscan(); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "scanning devices...", snackbar.length_long) .setaction("action", null).show(); scanfornetworks(view); } }); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); if(findviewbyid(r.id.fragment_container) != null){ if(savedinstancestate != null){ return; } navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); wififragment wififragment = new wififragment(); wififragment.setarguments(getintent().getextras()); fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace(r.id.fragment_container, wififragment); transaction.commit(); }else{ log.i("main_activity", "fragment_container null"); } }
my mainactivity
onnavigationitemselected()
method:
@override public boolean onnavigationitemselected(menuitem item) { // handle navigation view item clicks here. int id = item.getitemid();
if (id == r.id.nav_settings) { generalsettings generalsettings = new generalsettings(); bundle args = new bundle(); args.putint(generalsettings.arg_positon, 0); generalsettings.setarguments(args); fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace(r.id.fragment_container, generalsettings); transaction.addtobackstack(null); int stackid = transaction.commit(); log.i(this.getclass().getsimplename(), "stack id: " + stackid); }else if(id == r.id.nav_wlan_setting){ wifisettings wifisettings = new wifisettings(); bundle args = new bundle(); args.putint(generalsettings.arg_positon, 0); wifisettings.setarguments(args); fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace(r.id.fragment_container, wifisettings); transaction.addtobackstack(null); int stackid = transaction.commit(); log.i(this.getclass().getsimplename(), "stack id: " + stackid); }else if(id == r.id.nav_led_settings){ uvledsettings uvledsettings = new uvledsettings(); bundle args = new bundle(); args.putint(generalsettings.arg_positon,0); uvledsettings.setarguments(args); fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace(r.id.fragment_container, uvledsettings); transaction.addtobackstack(null); int stackid = transaction.commit(); log.i(this.getclass().getsimplename(), "stack id: " + stackid); }else if(id == r.id.nav_server_settings){ serversettings serversettings = new serversettings(); bundle args = new bundle(); // todo: fix args settings fragments args.putint(generalsettings.arg_positon, 0); serversettings.setarguments(args); fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace(r.id.fragment_container, serversettings); transaction.addtobackstack(null); int stackid = transaction.commit(); log.i(this.getclass().getsimplename(), "stack id: " + stackid); } else if (id == r.id.nav_current_device) { } else if (id == r.id.nav_available_devices){ wififragment wifi = new wififragment(); bundle args = new bundle(); args.putint(generalsettings.arg_positon, 0); wifi.setarguments(args); fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace(r.id.fragment_container, wifi); transaction.addtobackstack(null); int stackid = transaction.commit(); log.i(this.getclass().getsimplename(), "stack id: " + stackid); } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); return true; }
the problem fragment
:
package com.myapp.serviceapplication.fragments; import android.content.context; import android.net.wifi.scanresult; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.listview; import android.widget.relativelayout; import com.myapp.serviceapplication.r; import com.myapp.serviceapplication.adapters.wifiitemlistadapter; import java.util.arraylist; /** * fragment representing list of items. * <p> * activities containing fragment must implement {@link onlistfragmentinteractionlistener} * interface. */ public class wififragment extends fragment { // todo: customize parameter argument names private static final string arg_column_count = "column-count"; // todo: customize parameters private int mcolumncount = 1; private onlistfragmentinteractionlistener mlistener; /** * mandatory empty constructor fragment manager instantiate * fragment (e.g. upon screen orientation changes). */ public wififragment() { // required empty public constructor } // todo: customize parameter initialization @suppresswarnings("unused") public static wififragment newinstance(int columncount) { wififragment fragment = new wififragment(); bundle args = new bundle(); args.putint(arg_column_count, columncount); fragment.setarguments(args); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { mcolumncount = getarguments().getint(arg_column_count); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_wifi_list, container, false); if(view instanceof relativelayout){ context context = view.getcontext(); relativelayout relativelayout = (relativelayout) view; listview listview = (listview) relativelayout.findviewbyid(r.id.lst_wifi_items); listview.setadapter(new wifiitemlistadapter(this.getcontext(), new arraylist<scanresult>())); } return view; } @override public void onattach(context context) { super.onattach(context); // todo: needs modified correct listener type if (context instanceof onlistfragmentinteractionlistener) { mlistener = (onlistfragmentinteractionlistener) context; } else { throw new runtimeexception(context.tostring() + " must implement onlistfragmentinteractionlistener"); } } @override public void ondetach() { super.ondetach(); mlistener = null; } /** * interface must implemented activities contain * fragment allow interaction in fragment communicated * activity , potentially other fragments contained in * activity. * <p> * see android training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >communicating other fragments</a> more information. */ public interface onlistfragmentinteractionlistener { // todo: update argument type , name void onlistfragmentinteraction(view item); } }
the problem fragment
layout file:
<?xml version="1.0" encoding="utf-8"?> <relativelayout 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.wififragment" > <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lst_wifi_items" android:background="#ffffff"/> </relativelayout>
the content_main.xml
:
<?xml version="1.0" encoding="utf-8"?> <relativelayout 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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".activities.mainactivity" tools:showin="@layout/app_bar_main"> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </relativelayout>
the activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout 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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:opendrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.navigationview android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitssystemwindows="true" app:headerlayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.drawerlayout>
i've spent many hours trying figure out, can't see issue.
that's happen because fragment container above listview , default if not use background in viewgroup(linearlayout, relativelayout,...) that's gonna transparent, so, need put background in fragment container:
fragment_wifi_list.xml
<relativelayout 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.wififragment" android:background="#ffffff" > ...
tip:
android:clickable="true"
that's avoid concurrence clicks problems
Comments
Post a Comment