android - How to start Fragment from onClick -


i using recyclerview in app. want start fragment when click on image view. don't know how to. want put data when starting fragment. know how start activity below code. how can start fragment same way?

edited code

   fragmentmanager fm = getsupportfragmentmanager();    fragmenttransaction ft = fm.begintransaction();    ft.replace(r.id.layoutcontent, frag);    ft.commit(); 

fragments cannot started, must added container.
fragments aren't meant function on own, need enclosing activity.

having following layout:

    [...]     <framelayout android:id="@+id/container"             android:layout_width="match_parent"              android:layout_height="match_parent"             android:background="?android:attr/detailselementbackground" />     [...] 

you place fragment in such:

fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); transaction.replace(r.id.container, newfragment); transaction.commit(); 

you pass arguments fragment using bundle , creating fragment follows:

testfragment newfragment = new testfragment(); bundle args = new bundle(); args.putstring("hello world!"); newfragment.setarguments(args); 

this has done before transaction.

further info refer official documentation



note on edited code: have call transaction inside activity framelayout part of.
alternatively use rather dirty workaround: in main:

public class main extends activity{   public static main currentinstance;    public void oncreate(bundle boomerang){     currentinstance = this;   } } 

in playlist activity use main.currentinstance.getsupportfragmentmanager() etc.
wouldn't recommend it.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -