java - how to check if fragment exists and is Visible in a container? how to remove it if it exists? -


i calling method displays data on button click everytime. each time button clicked call method that

  • creates frag object
  • bundles data frag
  • puts data in frag
  • adds frag view in main activity
  • commits transaction

i'm facing 'caused by: java.lang.illegalstateexception: commit called'

on button click need check if frag exists , either replace or something. i'm confused.

here's code.

public void retrieve_and_display_data(string co) {      string res = null;       try {          res = new getvaluefromservicecall().execute(co, "", "").get();      } catch (interruptedexception e) {          e.printstacktrace();      } catch (executionexception e) {          e.printstacktrace();      }       list < string > company_stock_details = arrays.aslist(res.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1));       stock_price_frag frag_obj = new stock_price_frag();         bundle b = new bundle();      b.putstring("co_name", "data1");      b.putstring("co_first", "data2");      b.putstring("co_last", "data3");       frag_obj.setarguments(b);        ft.add(r.id.frag_container, frag_obj, "fragment");       ft.commit();       //error on commit  } 

fragmenttransaction.replace(r.id.fragment_container, fragment, mainactivity.tag); 

use tag when add fragment frame layout

and add check when button click

if (getsupportfragmentmanager().findfragmentbytag(mainactivity.tag) instanceof myfragment)       //my fragment visible  else       //other fragment visibile 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -