This question already has answers here:
Send data from activity to fragment in Android
(22 answers)
Closed 6 years ago.
I am beginner in android. I am trying to find a way to transfer the text from the editText of an an activity to the fragment so that the result is displayed in the textview of the fragment.
you can use bundle to transfer data from activity to fragment
I suppose data will be sent on button click.
Bundle bundle = new Bundle();
bundle.putString("from_activity", edittext.getText().toString());
Fragmentclass fragment= new Fragmentclass();
fragment.setArguments(bundle);
In fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
view=inflater.inflate(R.layout.fragment, container, false);
String text= getArguments().getString("from_activity");
TextView tv=(TextView)findViewById(R.id.yourTextView);
tv.setText(text);
return view;
}
Related
This question already has answers here:
How to pass values between Fragments
(18 answers)
Closed 1 year ago.
I have a button in my screen, I need to pass boolean value while click on the button.
Here while click on the button it starts a fragment and opens an another screen there I need to show only a button.
In the same way, the same button needs to show all the screen view If I navigate from an another fragment.
Can I able to send a boolean value along this and how it will be retrieved in another fragment?
case R.id.tv_send:
fragment = UserFragment.newInstance(this, mModel);
break;
user Argument to pass value fragment to fragment like this
put your Boolean value
fragment = UserFragment.newInstance(this, mModel);
Bundle b = new Bundle();
b.putBoolean("onPress",true);
fragment.setArguments(b);
get your value for another fragment
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
boolean getValue = getArguments().getBoolean("onPress");
}
This question already has answers here:
How to send data from one Fragment to another Fragment?
(4 answers)
Basic communication between two fragments
(15 answers)
Closed 5 years ago.
i'm pretty much stuck. I want to pass user input from the fields below
from one fragment, CarDetailsFragment to another fragment ConfirmationFragment. I also want to pass the states of the checkboxes(whether checked or unchecked). Kindly help.
Use Bundle to send String:
//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey1", "YourValue");
args.putString("YourKey2", "YourValue");
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
In onCreateView of the new Fragment:
//Retrieve the value
String value = getArguments().getString("YourKey");
Duplicate
Send data with Bundle as:
Bundle bundle = new Bundle();
bundle.putString("key", "value");
// set Fragmentclass Arguments
CarDetailsFragment carDetailsFragment = new CarDetailsFragment();
carDetailsFragment.setArguments(bundle);
//replace fragment
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, carDetailsFragment);
transaction.addToBackStack(null);
transaction.commit();
and in CarDetailsFragmentclass onCreateView() method:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String value = getArguments().getString("key");
return inflater.inflate(R.layout.fragment, container, false);
}
Use POJO class for this..
On Sending...
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putExtra("DATA", POJO_MODEL);
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
On Receiving...
POJO_MODEL pojo_model = POJO_MODEL_CAST)getArguments().getSerializable("DATA");
What I would recommend is to use a library like EventBus.
It's very lightweight and will make your life much easier. Please check the page I have linked above.
All you would need to do post an event and you create subscribe to those events in the whole app.
I have home activity which has 7 tabs. and for each tab I have created one fragment. Now I am transferring data from home fragment to other activity named Feature_Product_Activity which has 3 tabs, and here also for each tab I have created fragment. Then I am passing data from Feature_Product_Activity to one of the child fragment named FragmentFeatureOverview.
This is the method for passing data from Container Activity named Feature_Product_Activity to child Fragment named FragmentFeatureOverview
public void getValueFromFragment()
{
Bundle bundle = getIntent().getExtras();
getFeatureDescription= bundle.getString("desc");
Bundle b = new Bundle();
fragmentManager = getSupportFragmentManager();
b.putString("bb",getFeatureDescription);
FragmentFeatureOverview ff = new FragmentFeatureOverview();
ff.setArguments(b );
fragmentManager.beginTransaction().replace(R.id.fragmentContainer,ff).commit();
}
and in my fragment activity named FragmentFeatureOverview and getting data .
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_feature_overview, container, false);
TextView description = (TextView)view.findViewById(R.id.overview_Feature_description);
if(getArguments()!=null) {
String value = getArguments().getString("bb");
System.out.println("bundle is not null"+value);
description.setText(value);
}
return view;
}
I am getting the value in the bundle. But after setting the value in the TextView, Tabs Stops scrolling.
Any help will be greatly appreciated
This question already has answers here:
Send data from activity to fragment in Android
(22 answers)
Closed 6 years ago.
I have a query regarding fragment.
Scenario is: After I login, I am on an actiivity which have 3 fragments. For fragments I have used ViewPager. Now I have to use login username in one of my fragment. I have bought username from login page using putExtra. My query is how take that username to the fragments ??
From your BaseActivity send data with intent :
Bundle bundle = new Bundle();
bundle.putString("username", "From your BaseActivity");
// set Fragmentclass Arguments
Fragmentclass fragmentclass = new Fragmentclass();
fragmentclass .setArguments(bundle);
And in your Fragment onCreatView method :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String username= getArguments().getString("username");
return inflater.inflate(R.layout.yourFragment, container, false);
}
You can also use SharedPreferences.
Store the username or what so ever you want to save in SharedPreferences and use it anywhere in your app.
For your reference SharedPreferences Tutorials
From your activity you can send data with intent:
Bundle bundle = new Bundle();
bundle.putString("username", "abcd");
// set Fragmentclass Arguments
FragmentClass frag_one = new Fragmentclass();
frag_one.setArguments(bundle);
and add below code in Fragment onCreateView method:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("username");
return inflater.inflate(R.layout.fragment_layout, container, false);
}
I have a TextView into a fragment. When i update the text property from the activity, the component is not updated on fragment.
TextView amountTag = (TextView) fm.findFragmentByTag("currentFrag").getView().findViewById(R.id.amountTag);
amountTag.setText("another text");
fm variable is my FragmentManager.
I already tried to detach and attach the fragment again. But it does not work.
Check that you inflate your view in onCreateView of your fragment like this:
TextView amountTag;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_sample, container, false);
amountTag= (TextView) v.findViewById(R.id.amountTag);
return v;
}
If you have any issue create setAmountText in your fragment
public void setAmountText(String text) {
amountTag.setText(text);
}
An call it in your Activity
((CurrentFragment)fm.findFragmentByTag("currentFrag")).setAmountText("newValue");
Hope this helps!
I found the answer to problem here:
Handling onNewIntent in Fragment
The solution is send params to fragment, like:
Bundle bundle = new Bundle();
bundle.putString("amount", "newValue");
fragment.setArguments(bundle);
In the fragment:
Bundle bundle = this.getArguments();
if (bundle != null) {
amountTag.setText(bundle.getString("amount", "defaultValue"));
}
Thanks to everyone that helped!