android - Dynamic replacing of a fragment is not working -
i know there lot of posts cant find solution. i'm trying replace fragments on activity dynamically, have followed lots of tutorials , posts here , got stuck in 1 exception:
" must implement onfragmentinteractionlistener"
i have looked i'm getting tired of this.
more exception occurs in fragmenttransaction.replace(r.id.content_frame,fragment);
line, must missed on main activity cant see it.
here of code..
public class loggedactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { private framelayout fragment; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_logged); this.settitle(r.string.app_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); 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(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); } @override public void onbackpressed() { drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawer(gravitycompat.start); } else { super.onbackpressed(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.logged, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) { // handle navigation view item clicks here. int id = item.getitemid(); if (id == r.id.nav_consultas) { android.support.v4.app.fragmentmanager fragmentmanager = getsupportfragmentmanager(); android.support.v4.app.fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragment fragment = new consultasfragment(); fragmenttransaction.replace(r.id.content_frame,fragment); fragmenttransaction.commit(); //setcontentview(r.layout.activity_main); // handle camera action } else if (id == r.id.nav_trans) { } else if (id == r.id.nav_pagos) { } else if (id == r.id.nav_telefono) { } else if (id == r.id.nav_creditos) { } else if (id == r.id.nav_mapa) { } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); return true; }
}
my container activity 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"> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <include layout="@layout/app_bar_logged" 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_logged" app:menu="@menu/activity_logged_drawer" /> </android.support.v4.widget.drawerlayout>
and fragment activity
public class consultasfragment extends fragment { // todo: rename parameter arguments, choose names match // fragment initialization parameters, e.g. arg_item_number private static final string arg_param1 = "param1"; private static final string arg_param2 = "param2"; // todo: rename , change types of parameters private string mparam1; private string mparam2; private onfragmentinteractionlistener mlistener; public consultasfragment() { // required empty public constructor } /** * use factory method create new instance of * fragment using provided parameters. * * @param param1 parameter 1. * @param param2 parameter 2. * @return new instance of fragment consultasfragment. */ // todo: rename , change types , number of parameters public static consultasfragment newinstance(string param1, string param2) { consultasfragment fragment = new consultasfragment(); bundle args = new bundle(); args.putstring(arg_param1, param1); args.putstring(arg_param2, param2); fragment.setarguments(args); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { mparam1 = getarguments().getstring(arg_param1); mparam2 = getarguments().getstring(arg_param2); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.fragment_consultas, container, false); } // todo: rename method, update argument , hook method ui event public void onbuttonpressed(uri uri) { if (mlistener != null) { mlistener.onfragmentinteraction(uri); } } @override public void onattach(context context) { super.onattach(context); if (context instanceof onfragmentinteractionlistener) { mlistener = (onfragmentinteractionlistener) context; } else { throw new runtimeexception(context.tostring() + " must implement onfragmentinteractionlistener"); } } @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 onfragmentinteractionlistener { // todo: update argument type , name void onfragmentinteraction(uri uri); } }
xml...
<framelayout 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="layout.consultasfragment"> <!-- todo: update blank fragment layout --> <textview android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </framelayout>
most of code generated automatically android studio.
change loggedactivity
declaration
public class loggedactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener, consultasfragment.onfragmentinteractionlistener
then override method in loggedactivity
:
@override public void onfragmentinteraction(uri uri) { // whatever activity when fragment calls }
the point here need provide mechanism fragment
interact activity
, calling mlistener.onfragmentinteraction(uri)
in fragment
, can call activity
whatever require.
Comments
Post a Comment