android - Switch from Activity to Fragment -


i have problem using fragments within activity. i'd step fragment activity , i'm close this. here activity:

utils u; fragment fragment; framelayout framelayout; button btn;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      btn = (button)findviewbyid(r.id.galleryid);     framelayout = (framelayout)findviewbyid(r.id.fragmentcontainer);      btn.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {              try {                 fragment = new galleryfragment();                 utils.fragmentmanager(framelayout.getid(), fragment, mainactivity.this);             } catch (exception e) {                 e.getstacktrace();             }         }     }); 

utils:

public class utils { public static void fragmentmanager(integer contentid, fragment fragment, activity activity) {     try {         fragmentmanager fm = activity.getfragmentmanager();         fragmenttransaction fragmenttransaction = fm.begintransaction();         fragmenttransaction.replace(contentid, fragment);         fragmenttransaction.commit();     } catch (exception e) {         e.printstacktrace();     } } } 

galleryfragment:

 public class galleryfragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup parent, bundle savedinstancestate) {     // defines xml file fragment     return inflater.inflate(r.layout.fragment_galleryfragment, parent, false); }  // event triggered after oncreateview(). // view setup should occur here.  e.g., view lookups , attaching view listeners. @override public void onviewcreated(view view, bundle savedinstancestate) {     // setup handles view objects here     // edittext etfoo = (edittext) view.findviewbyid(r.id.etfoo); } 

}

and xmls activity_main:

 <?xml version="1.0" encoding="utf-8"?>  <relativelayout 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" >  <framelayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_alignparenttop="true"     android:id="@+id/fragmentcontainer"     android:layout_centerhorizontal="true">  </framelayout>  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/galleryid"     android:text="gallery"     android:layout_centerhorizontal="true"/>  </relativelayout> 

and fragment_galleryfragment.xml:

  <?xml version="1.0" encoding="utf-8"?>  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@drawable/ok">  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content" />   </linearlayout> 

when click on gallery button layout of gallery appears on screen. activity_main layout stays is. i'd switch layout switching activity activity b. can this?

try using

fragmentmanager fm = activity.getsupportfragmentmanager(); //instead of fragmentmanager fm = activity.getfragmentmanager(); 

rest of code looks correct.

you should use

getsupportfragmentmanager()

if you're using

android.support.v4.app.fragmentmanager

and if using

android.app.fragmentmanager

then should use

getfragmentmanager()


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -