How to send data from Activity to Fragment

前端 未结 5 1837
盖世英雄少女心
盖世英雄少女心 2020-12-05 21:44

I know there are many topics about this here. I have also read documentation many times but I can\'t find the best way to pass data from activity to fragment.

I want

相关标签:
5条回答
  • 2020-12-05 21:56
    Bundle bundle = new Bundle();
    bundle.putString("edttext", "From Activity");
    // set Fragmentclass Arguments
    Fragmentclass fragobj = new Fragmentclass();
    fragobj.setArguments(bundle);
    

    and in Fragment onCreateView method:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        String strtext = getArguments().getString("edttext");    
        return inflater.inflate(R.layout.fragment, container, false);
    }
    

    see detail answer here..

    0 讨论(0)
  • 2020-12-05 21:58

    Find fragment in Activity onCreate and set data to a method you write in your fragment:

            ExampleFragment rf =  (ExampleFragment) getSupportFragmentManager().findFragmentById(R.id.exampleFragment);
    
        if(rf!=null){
        rf.setExample(currentExample);
        }
    

    "CurrentExample" is whatever you want to send in to your "setExample" method in your fragment.

    public void setExample(ExampleObject currentExample){
    
        currentExampleInFragment = currentExample;
    
    }
    

    You can use the data in onActivityCreated method of Fragment.

    Not sure is this is a good solution or not, but I found it the easiest one for passing objects.

    0 讨论(0)
  • 2020-12-05 22:09

    "send data from Activity to Fragment"

    Activity:

            Bundle bundle = new Bundle();
            bundle.putString("message", "Alo Stackoverflow!");
            FragmentClass fragInfo = new FragmentClass();
            fragInfo.setArguments(bundle);
            transaction.replace(R.id.fragment_single, fragInfo);
            transaction.commit();
    

    Fragment:

    Reading the value in the fragment

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Bundle bundle = this.getArguments();
            String myValue = bundle.getString("message");
            ...
            ...
            ...
            }
    

    or

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            String myValue = this.getArguments().getString("message");
            ...
            ...
            ...
            }
    
    0 讨论(0)
  • 2020-12-05 22:09

    Create one Session model class and in Activity set the values you want to data needs to be send then in Fragment you can get that values from Session model class

    eg. from your activity u can set like this.

     AllEventDetails.getInstance().setEvent_description(event_Description);
    AllEventDetails.getInstance().setDj_name(dj_name);
    AllEventDetails.getInstance().setMusic_theme(music_theme);
    AllEventDetails.getInstance().setClub_name(club_name);
    AllEventDetails.getInstance().setDate(event_date);
    AllEventDetails.getInstance().setBanner_image_path(banner_image_path);
    AllEventDetails.getInstance().setEvent_title(event_title);  
    

    and from your Fragment u can retrive like this.

    AllEventDetails.getInstance().getClub_name()
    .........
    

    Creating Session model class is like this.

    public class AllEventDetails {
        private static AllEventDetails mySession ;
        private String event_description;
        private String dj_name;
        private String music_theme;
        private String club_name;
        private String date;
        private String banner_image_path;
        private String event_title;
    
        private AllEventDetails() {     
            event_description = null;
            dj_name = null;
            music_theme = null;
            club_name = null;
            date = null;
            banner_image_path = null;
            event_title = null;
    
        }
    
        public static AllEventDetails getInstance() {
            if( mySession == null ) {
                mySession = new AllEventDetails() ;
            }
            return mySession ;
        }
    
        public void resetSession() {
            mySession=null;
        }
        public String getEvent_description() {
            return event_description;
        }
        public void setEvent_description(String event_description) {
            this.event_description = event_description;
        }
        public String getDj_name() {
            return dj_name;
        }
        public void setDj_name(String dj_name) {
            this.dj_name = dj_name;
        }
        public String getMusic_theme() {
            return music_theme;
        }
        public void setMusic_theme(String music_theme) {
            this.music_theme = music_theme;
        }
        public String getClub_name() {
            return club_name;
        }
        public void setClub_name(String club_name) {
            this.club_name = club_name;
        }
        public String getDate() {
            return date;
        }
        public void setDate(String date) {
            this.date = date;
        }
        public String getBanner_image_path() {
            return banner_image_path;
        }
        public void setBanner_image_path(String banner_image_path) {
            this.banner_image_path = banner_image_path;
        }
        public String getEvent_title() {
            return event_title;
        }
        public void setEvent_title(String event_title) {
            this.event_title = event_title;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-05 22:10

    Usually the activities will have a reference to their fragments. In your SearchableActivity.java are you also loading PlaceListFragment.java either in setContentView(activity_searchable.xml); or you need to create a instance of the fragment and add/replace a fragment using FragmentTransaction.

    you can find a good example here on how to communicated between fragments or between activity & fragment.

    Training link

    0 讨论(0)
提交回复
热议问题