Get EditText value on different fragment

后端 未结 2 2029
一向
一向 2021-01-22 08:27

I am trying to get some text from editTexts on different fragments. So what I do first is define my mPager and mPagerAdapter:

2条回答
  •  故里飘歌
    2021-01-22 08:50

    Try this:

    In your second fragment, pass the intent extras and your value. For example:

    In you second fragment, do this :

    Intent someintent = new Intent();
            //Some first result
            someintent.putExtra("your_value_one", your_value_one);
            //Some Second result
            someintent.putExtra("your_value_two", your_value_two);
            //Some third result
            someintent.putExtra("your_value_three", your_value_three);
            getActivity().setResult(getActivity().RESULT_OK,someintent);
            getActivity().finish();
    

    In your other fragment, where you want this result, do this on your other fragment like this:

    1) Make some method to get the info.

    private void getInfo() {
    
                        //Note: The values are coming from a diff activity or fragment and                 so the strings should match.
                        Intent data = new Intent();
                String first_value = data.getStringExtra("your_value_one");
                        String second_value = data.getStringExtra("your_value_two");
                        String third_value = data.getStringExtra("your_value_three");
                Log.i("First Value: ", first_value);
                        Log.i("First Value: ", second_value);
                        Log.i("First Value: ", third_value);
    
            }
    

    After making this method, just call it on your onActivityCreated().

    Now you can use those string however and wherever you want to. If you want to use those values anywhere else, make sure to define you strings at the very start so that you can use the values anywhere.

    Hope this answer helps .. :)

提交回复
热议问题