Android Share data between two Fragment - android

I am new developer of Android.How we can share the data between two fragment.
Any idea for this problem.i have make the orderFragment and itemfragment user select the item on item screen and press submit the screen redirct to orders screen user have multiple order.if we redirect the screen the data not send to the orders screen

For Send data between two fragment try below code
YourSubmitButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Bundle bundle = new Bundle();
bundle.putString = ("KEY",value) //Here if you want to send Integer value then write putInt
YourItemFragment.setArguments(bundle);
}
}
In ItemFragment try below code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_order, container, false);
String YourItem = getArguments.getString("KEY");
return view;
}

Its simple to send data between fragments try this
Bundle bundle = new Bundle();
bundle.putString("YourKeyHere",value);
itemFragment.setArguments(bundle);
And then in order Fragment receive as
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String item = getArguments().getString("YourKeyHere");
return inflater.inflate(R.layout.fragment, container, false);
}

Related

After getting intent value from fragment how can i implement in recyclerview item click

I set a button in recyclerview itemclick , After clicking button fragment will be open ,and I get the fragment value perfectly and pass that value through intent ,but my problem is how can I implement get extra intent in recyclerview adapter and set value to that particular itemclick. Please help me out..
Pass Data From Activity:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
Get Data From Activity:
String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
Send Data From Fragment:
Bundle bundle = new Bundle();
bundle.putString("message", "From Activity");
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
Get Data From Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("message");
return inflater.inflate(R.layout.fragment, container, false);
}
Send Data From Adapter to Fragment/Activity:
holder.itemView.setOnClickListener {
val bundle = Bundle()
bundle.putString("id",data[position]?.id)
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
}
Get Data From Adapter to Fragment/Activity:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("id");
return inflater.inflate(R.layout.fragment, container, false);
}

How to know from which instance of Fragment the setOnCheckedChangeListener is called?

I have a main activity in which i have to display 3out of 12 questions in each screen (which are saved in sql database) and with radio buttons Yes and No .
So i made a fragment class and call it thrice in my main activity .
On my next button i create and intent of my main class .
hence with one fragment i populate 12 questions in 4 screens .
Now , when i press the radio button , i want to see for which question the radio button is clicked and then calculate the score (each question has separate score. this is also mapped in my sqlite database against each question )How do i do that ?
Method 1: Create 4 Fragments.
public class Fragment1 extends Fragment {
private RadioButton radioButton;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// radioButton.setOnCheckedChangeListener...
}
}
public class Fragment2 extends Fragment {
private RadioButton radioButton;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// radioButton.setOnCheckedChangeListener...
}
}
public class Fragment3 extends Fragment {
private RadioButton radioButton;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// radioButton.setOnCheckedChangeListener...
}
}
public class Fragment4 extends Fragment {
private RadioButton radioButton;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// radioButton.setOnCheckedChangeListener...
}
}
Method 2: Create 1 Fragment with passing 4 different integer bundle.
Bundle bundle1 = new Bundle();
bundle1.putInt("questions1" , 1);
Bundle bundle2 = new Bundle();
bundle2.putInt("questions2" , 2);
Bundle bundle3 = new Bundle();
bundle3.putInt("questions3" , 3);
Bundle bundle4 = new Bundle();
bundle4.putInt("questions4" , 4);
Fragment fragment = new QuestionsFragment();
fragment.setArguments(bundle1);
Fragment fragment = new QuestionsFragment();
fragment.setArguments(bundle2);
Fragment fragment = new QuestionsFragment();
fragment.setArguments(bundle3);
Fragment fragment = new QuestionsFragment();
fragment.setArguments(bundle4);
and onCreateView in QuestionsFragment use Bundle bundle1 = getArguments(); for get integer flags.

Passing Data from acitvity to fragment android studio

--This is my Fragment :--
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_save, container, false);
savedAlamat = view.findViewById(R.id.saved_alamat);
savedContac = view.findViewById(R.id.saved_kontak);
savedName = view.findViewById(R.id.saved_nama);
String malamat = getArguments().getString("mAlamat");
String mcontac = getArguments().getString("mContac");
String mname = getArguments().getString("mname");
savedAlamat.setText(malamat);
savedContac.setText(mcontac);
savedName.setText(mname);
return view;
}
//and This is main acivity that constarin framelayout :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_save, container, false);
savedAlamat = view.findViewById(R.id.saved_alamat);
savedContac = view.findViewById(R.id.saved_kontak);
savedName = view.findViewById(R.id.saved_nama);
String malamat = getArguments().getString("mAlamat");
String mcontac = getArguments().getString("mContac");
String mname = getArguments().getString("mname");
savedAlamat.setText(malamat);
savedContac.setText(mcontac);
savedName.setText(mname);
return view;
}
--I don't understand why I keep get NotNULLobject, i want to make a passing data between that activity and fragment.
In Activity, write this code
Bundle bundle = new Bundle();
bundle.putString("mAlamat", "Data to send"); // set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
In Fragment class, receive data like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("mAlamat");
return inflater.inflate(R.layout.fragment, container, false);
}
Courtesy

How to transfer value from one fragment to another fragment

Example
I do not understand this example.
How to apply in my program
I try to write In my program
General use that I can.
But replaced fragment.
I do not know how to use.
Pass the value of the object.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.templistmune, container, false);
FragmentManager DS2 = getFragmentManager();
FragmentTransaction DSE2 = DS2.beginTransaction();
Fragment DF2 = DS2.findFragmentById(R.id.frameLayout4);
if (DF2 == null) {
String title = "Fragment A";
templistview2 usermune = new templistview2(title);
DSE2.add(R.id.frameLayout4, usermune);
DSE2.addToBackStack(null);
DSE2.commit();
/////////////////////////////////////////////////////////
Bundle bundle = new Bundle();
String SAS="50";
bundle.putString("ST", SAS);
DF2.setArguments(bundle);
////////////////////////////////////////////////
}
The pick value the object
templistview2.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.templistmune2, container, false);
////////////////////////////////////////////////////////////////////////////////////
Bundle bundle = this.getArguments();
String myST = bundle.getString("ST", SAS);
///////////////////Error SAS cannot be resolved to a variable /////////////////
return view;
}
You're only adding adding the Bundle when DF2 is null... so your program will either crash with a NullPointerException somewhere along the line or run but not have the Bundle passed.
So place
Bundle bundle = new Bundle();
String SAS="50";
bundle.putString("ST", SAS);
DF2.setArguments(bundle);
outside the if structure.

Android Fragment Implementation

I am trying to implement Fragments in my Project using the https://github.com/JakeWharton/Android-ViewPagerIndicator plugin.
My Fragment
public final class NearByFragment extends Fragment {
private static String TAG = "bMobile";
public static NearByFragment newInstance(String content) {
NearByFragment fragment = new NearByFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
return inflater.inflate(R.layout.fragment_search_nearby, null);
}
}
Now I want to execute some code like start start a new thread and download a JSON from the server and then assign a list view from the content I download. In fragments we are assigning views in onCreateView. so where should I write the
listview = (ListView) findViewById(R.id.list_items);
((TextView) findViewById(R.id.title_text))
.setText(R.string.description_blocked);
and other code to generate the Fragment view ?
You can search within the view that you just inflated:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
View v = inflater.inflate(R.layout.fragment_search_nearby, null);
listview = (ListView)v.findViewById(R.id.list_items);
((TextView)v.findViewById(R.id.title_text))
.setText(R.string.description_blocked);
return v;
}
You can also use the Fragment.getView() function elsewhere in your code and call that view's findViewById() member.
You can use getActivity().findByView() from the Fragment if you want to have access to the layout elements. Alternatively, you can just put the findByView() call in the main Activity.

Categories

Resources