Send data from activity to fragment in Android

前端 未结 20 2791
悲哀的现实
悲哀的现实 2020-11-21 05:13

I have two classes. First is activity, second is a fragment where I have some EditText. In activity I have a subclass with async-task and in method doInBa

20条回答
  •  Happy的楠姐
    2020-11-21 05:35

    I would like to add for the beginners that the difference between the 2 most upvoted answers here is given by the different use of a fragment.

    If you use the fragment within the java class where you have the data you want to pass, you can apply the first answer to pass data:

    Bundle bundle = new Bundle();
    bundle.putString("edttext", "From Activity");
    Fragmentclass fragobj = new Fragmentclass();
    fragobj.setArguments(bundle);
    

    If however you use for example the default code given by Android Studio for tabbed fragments, this code will not work.

    It will not work even if you replace the default PlaceholderFragment with your FragmentClasses, and even if you correct the FragmentPagerAdapter to the new situation adding a switch for getItem() and another switch for getPageTitle() (as shown here)

    Warning: the clip mentioned above has code errors, which I explain later here, but is useful to see how you go from default code to editable code for tabbed fragments)! The rest of my answer makes much more sense if you consider the java classes and xml files from that clip (representative for a first use of tabbed fragments by a beginner scenario).

    The main reason the most upvoted answer from this page will not work is that in that default code for tabbed fragments, the fragments are used in another java class: FragmentPagerAdapter!

    So, in order to send the data, you are tempted to create a bundle in the MotherActivity and pass it in the FragmentPagerAdapter, using answer no.2.

    Only that is wrong again. (Probably you could do it like that, but it is just a complication which is not really needed).

    The correct/easier way to do it, I think, is to pass the data directly to the fragment in question, using answer no.2. Yes, there will be tight coupling between the Activity and the Fragment, BUT, for tabbed fragments, that is kind of expected. I would even advice you to create the tabbed fragments inside the MotherActivity java class (as subclasses, as they will never be used outside the MotherActivity) - it is easy, just add inside the MotherActivity java class as many Fragments as you need like this:

     public static class Tab1 extends Fragment {
    
        public Tab1() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.your_layout_name_for_fragment_1, container, false);
            return rootView;
        }
    }.
    

    So, to pass data from the MotherActivity to such a Fragment you will need to create private Strings/Bundles above the onCreate of your Mother activity - which you can fill with the data you want to pass to the fragments, and pass them on via a method created after the onCreate (here called getMyData()).

    public class MotherActivity extends Activity {
    
        private String out;
        private Bundle results;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_mother_activity);
    
           // for example get a value from the previous activity
            Intent intent = getIntent();
            out = intent.getExtras().getString("Key");
    
        }
    
        public Bundle getMyData() {
            Bundle hm = new Bundle();
            hm.putString("val1",out);
            return hm;
        }
    }
    

    And then in the fragment class, you use getMyData:

    public static class Tab1 extends Fragment {
            /**
             * The fragment argument representing the section number for this
             * fragment.
             */
            public Tab1() {
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.your_layout_name_for_fragment_1, container, false);
                TextView output = (TextView)rootView.findViewById(R.id.your_id_for_a_text_view_within_the_layout);
    
                MotherActivity activity = (MotherActivity)getActivity();
    
                Bundle results = activity.getMyData();
                String value1 = results.getString("val1");
    
                output.setText(value1);
                return rootView;
            }
        }
    

    If you have database queries I advice you to do them in the MotherActivity (and pass their results as Strings/Integers attached to keys inside a bundle as shown above), as inside the tabbed fragments, your syntax will become more complex (this becomes getActivity() for example, and getIntent becomes getActivity().getIntent), but you have also the option to do as you wish.

    My advice for beginners is to focus on small steps. First, get your intent to open a very simple tabbed activity, without passing ANY data. Does it work? Does it open the tabs you expect? If not, why?

    Start from that, and by applying solutions such as those presented in this clip, see what is missing. For that particular clip, the mainactivity.xml is never shown. That will surely confuse you. But if you pay attention, you will see that for example the context (tools:context) is wrong in the xml fragment files. Each fragment XML needs to point to the correct fragment class (or subclass using the separator $).

    You will also see that in the main activity java class you need to add tabLayout.setupWithViewPager(mViewPager) - right after the line TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); without this line, your view is actually not linked to the XML files of the fragments, but it shows ONLY the xml file of the main activity.

    In addition to the line in the main activity java class, in the main activity XML file you need to change the tabs to fit your situation (e.g. add or remove TabItems). If you do not have tabs in the main activity XML, then possibly you did not choose the correct activity type when you created it in the first place (new activity - tabbed activity).

    Please note that in the last 3 paragraphs I talk about the video! So when I say main activity XML, it is the main activity XML in the video, which in your situation is the MotherActivity XML file.

提交回复
热议问题