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);
}
Related
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.
This question already has answers here:
Send data from activity to fragment in Android
(22 answers)
Closed 5 years ago.
I have passed some key and value data from one activity to another activity fragment so I have not get key and value to the last point in the fragment.
I have passing data using bundle.
In your activity create bundle to set to the fragment
Yourfragment fragment = new Yourfragment();
Bundle args = new Bundle();
args.putString(ARG_DATA, data);
fragment.setArguments(args);
Then load the fragment getSupportFragmentManager().beginTransaction().replace(R.id.your_container,fragment).commit();
Then in your fragment oncreate get the data like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mData = getArguments().getString(ARG_DATA);
}
}
From activity to activity you can pass data by using Intent and when you get data in second activity in which you are creating fragments on creating fragment pass that data to fragment by making constructor in fragment or by using bundle. For further assistance and if you dont know how to do this do let me know.
Pass data from activity to activity:
Intent intent = new Intent(this, Second.class);
intent.putExtra("data", sessionId);
startActivity(intent);
get data in Second activity:
String s = getIntent().getStringExtra("data");
Pass data from activity to fragment:
Bundle bundle = new Bundle();
bundle.putString("data", "From Activity");
// set Fragmentclass Arguments
FragmentOne fragment = new FragmentOne ();
fragment.setArguments(bundle);
get data from activity to fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("data");
return inflater.inflate(R.layout.fragment, container, false);
}
Happy coding!!
You can pass data between Activity and Fragments and Fragment to fragment using below:
https://developer.android.com/training/basics/fragments/communicating.html
But to pass it between activity, you may use bundle data, (very few variables) or use Application Class to store it in memory, make sure you do not bloat up your memory.
New Android architecture components also do provide good options, it all depends upon your use:
https://developer.android.com/topic/libraries/architecture/index.html
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;
}
I am trying to pass string from Adapter to fragment in Bundle bu ti'm getting null in my Fragment
Here is my code to add string to Bundle
Bundle bundle=new Bundle();
bundle.putString("id",expense_id);
AddExpenseFragment fragment=new AddExpenseFragment();
fragment.setArguments(bundle);
UiActivity.startAddExpense(context);
this is my code to retrieve to Bundle value in Fragment
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view=inflater.inflate(R.layout.activity_fragment_add_expense,container,false);
String id=this.getArguments().getString("id");
return view;
}
and i'm passing bundle value from My recyclerView Adapter class
The problem is simple here. You are creating a fragment and setting data to it which is fine,
AddExpenseFragment fragment=new AddExpenseFragment();
fragment.setArguments(bundle);
But the problem is in your
UiActivity.startAddExpense(context);
you must be replacing with a new instance of the AddExpenseFragment as you are no where passing the newly created fragment instance (with the arguments set to it) to the startAddExpense() method.
The solution is simple, just pass the newly created fragment object to the startAddExpense() like this,
UiActivity.startAddExpense(context, fragment);
and then inside the method replace or add this new fragment and do not instantiate any new fragment object.
Try this:
Bundle bundle = this.getArguments();
String id=bundle.getString("id");
From your code, it's hard to find problems.just give your some suggestions.
Firstly, check whether your variable expense_id is null or not.
Secondly, check whether the AddExpenseFragment you created and really showed are the same.
I want to receive data from class to fragment by Intent ,I try to do ,I write
Intent n = this.{getIntent()};
{the wrong here} ,but this code is not working ,so what i do ?
Activity Send Intent data to fragment
Bundle b = new Bundle();
b.putString("data", "abc");
// set Fragmentclass Arguments
Fragment frag = new Fragment();
frag.setArguments(b);
Fragment receive data with intent
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String data = getArguments().getString("data");
return inflater.inflate(R.layout.fragment, container, false);
}
For fragments, you can visit Send data from activity to fragment in android
Try this,
you can send data from activity using,
Bundle bundle = new Bundle();
bundle.putString("key", "value");
Fragmentclass fc= new Fragmentclass();
fc.setArguments(bundle);
and receive from this way in your fragment's onCreateView method,
String strtext = getArguments().getString("key");
that's it