Send data from activity to fragment in Android

前端 未结 20 2706
悲哀的现实
悲哀的现实 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条回答
  • 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.

    0 讨论(0)
  • 2020-11-21 05:35

    If an activity needs to make a fragment perform an action after initialization, the easiest way is by having the activity invoke a method on the fragment instance. In the fragment, add a method:

    public class DemoFragment extends Fragment {
      public void doSomething(String param) {
          // do something in fragment
      }
    }
    

    and then in the activity, get access to the fragment using the fragment manager and call the method:

    public class MainActivity extends FragmentActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            DemoFragment fragmentDemo = (DemoFragment) 
                getSupportFragmentManager().findFragmentById(R.id.fragmentDemo);
            fragmentDemo.doSomething("some param");
        }
    }
    

    and then the activity can communicate directly with the fragment by invoking this method.

    0 讨论(0)
  • 2020-11-21 05:35

    Smartest tried and tested way of passing data between fragments and activity is to create a variables,example:

    class StorageUtil {
      public static ArrayList<Employee> employees;
    }
    

    Then to pass data from fragment to activity, we do so in the onActivityCreated method:

    //a field created in the sending fragment
    ArrayList<Employee> employees;
    
    @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
             employees=new ArrayList();
    
           //java 7 and above syntax for arraylist else use employees=new ArrayList<Employee>() for java 6 and below
    
         //Adding first employee
            Employee employee=new Employee("1","Andrew","Sam","1984-04-10","Male","Ghanaian");
            employees.add(employee);
    
          //Adding second employee
           Employee employee=new Employee("1","Akuah","Morrison","1984-02-04","Female","Ghanaian");
             employees.add(employee);
    
            StorageUtil.employees=employees;
        }
    

    Now you can get the value of StorageUtil.employees from everywhere. Goodluck!

    0 讨论(0)
  • 2020-11-21 05:37

    In your activity declare static variable

    public static HashMap<String,ContactsModal> contactItems=new HashMap<String, ContactsModal>();
    

    Then in your fragment do like follow

    ActivityName.contactItems.put(Number,contactsModal);
    
    0 讨论(0)
  • 2020-11-21 05:38

    The best and convenient approach is calling fragment instance and send data at that time. every fragment by default have instance method

    For example : if your fragment name is MyFragment

    so you will call your fragment from activity like this :

    getSupportFragmentManager().beginTransaction().add(R.id.container, MyFragment.newInstance("data1","data2"),"MyFragment").commit();
    

    *R.id.container is a id of my FrameLayout

    so in MyFragment.newInstance("data1","data2") you can send data to fragment and in your fragment you get this data in MyFragment newInstance(String param1, String param2)

    public static MyFragment newInstance(String param1, String param2) {
            MyFragment fragment = new MyFragment();
            Bundle args = new Bundle();
            args.putString(ARG_PARAM1, param1);
            args.putString(ARG_PARAM2, param2);
            fragment.setArguments(args);
            return fragment;
        }
    

    and then in onCreate method of fragment you'll get the data:

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getArguments() != null) {
                mParam1 = getArguments().getString(ARG_PARAM1);
                mParam2 = getArguments().getString(ARG_PARAM2);
            }
        }
    

    so now mParam1 have data1 and mParam2 have data2

    now you can use this mParam1 and mParam2 in your fragment.

    0 讨论(0)
  • 2020-11-21 05:38

    just stumbled across this question, while most of the methods above will work. I just want to add that you can use the Event Bus Library, especially in scenarios where the component (Activity or Fragment) has not been created, its good for all sizes of android projects and many use cases. I have personally used it in several projects i have on playstore.

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