I have simple app that has 1 activity and two fragments one of them display RecyclerView with OnClickListener to replace the fragment
now I need to send data from that arraylist RecyclerView with the click to other fragment and I don't know where I must type the Bundle and how
Your main culprit is ft1.replace(R.id.MainFram,SubMark()) instead you must use ft1.replace(R.id.MainFram,subMark) subMark variable. You are calling new instance of Fragment instead of fragment with bundle. Use these piece of code
val bundle = Bundle()
bundle.putString("data", "nasser")
val subMark = SubMark()
subMark.arguments = bundle
var ft1 : FragmentTransaction = supportFragmentManager.beginTransaction()
ft1.replace(R.id.MainFram,subMark) ft1.commit()
And access data with
String name = getArguments().getString("key");
Related
I have an activity and 2 fragments on it, this activity got value from intent and I want to pass this value to a fragment, but when I do this with
supportFragmentManager.beginTransaction().replace(R.id.nav_host_fragment,fragment).commit()
it overrides fragments on each other,(I have a fragment and bottomnavbar)
I think there should be a better way, for example passing data without replacing, but when I do this there is no value given to fragment.
Activity Code: `
val bundle = Bundle()
val fragment = ResultFragment()
bundle.putString("BMI", bmi)
fragment.arguments = bundle
supportFragmentManager.beginTransaction().replace(R.id.nav_host_fragment,fragment).commit()
Fragment code:
val data = arguments
val value = data?.getString("BMI")
I tried using different ways to navigate between fragments, but did not work.
while getting data in fragment use requireArguments()
if (arguments != null) {
if (requireArguments().getString("BMI")) {
// your code
}
I want to create same fragment multiple times in view pager with different arguments how can i do it?
Now i have a static single argument constructor in a fragment which i call to initialize the fragment
Is there a simple way to do this.
You could use setArguments() method, for each of your Fragment. Do something like this:
Fragment myFragment = new Fragment();
Bundle data = new Bundle();
data.putString("data_1","Hello");
myFragment.setArguments(data);
Then in your Fragment, do this:
Bundle data = getArguments();
String data_1 = data.getString("data_1");
So basically I'm using same type of fragment in two different activities and I want to create and initialize some variable in the fragment only if it was added from specific activity. My question is how can I programmatically find out in which activity the fragment was added.
there're two main ways of achieving it:
the less modular approach, you simply check using instanceof
if(getActivity() instanceof MyActivity)
and the more modular approach, you pass some arguments to the fragment on the moment you'll add it to the transaction:
// this during the transaction to pass extra parameters to the fragment
Fragment f = new MyFragment();
Bundle b = new Bundle();
b.putBoolean("doExtraCode", true);
f.setArguments(b);
then inside the fragment:
// check if should execute extras
Bundle b = getArguments();
boolean doExtraCode = b == null? false: b.getBoolean("doExtraCode", false);
I have an activity that calls to a web service and I want to pass these result to a fragment. Obviously the web service is invoked by an AsyncTask, so the fragment is loaded before getting result.
How can I pass this paramteter from activity's AsyncTask to fragment when is received?
You can implement a method inside your Fragment and call it when needed. For bidirectional communication between an Activity and a Fragment see http://developer.android.com/training/basics/fragments/communicating.html
Set bundle in your fragment.
Bundle args = new Bundle();
args.putInt("id",value);
Fragment newFragment = new Fragment();
newFragment.setArguments(args);
In your fragment get the bundle as
Bundle b = getArguments();
String s = b.getInt("id");
You could move the AsyncTask into the fragment.
But if you wish to keep your current set up, you should save the Fragment reference when you initialize it, and create a public function in the fragment that takes the new data as a parameter.
So the activity code could look like this:
MyFragment fragment = new MyFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
and then you could call:
fragment.updateData(myNewData);
Just make sure to do the appropriate null-checks, just to be safe.
I currently have this Custom ArrayList:
ArrayList<PlaceDetails> place_list = new ArrayList<PlaceDetails>();
which will be populated during the onCreateView() portion.
I am unsure as of how do I pass this ArrayList in a bundle from this fragment class to another fragment class. Below is the snippet of my codes:
public void Map(View view){
if(hasConnection() == true){
Bundle b = new Bundle();
// how should I be passing the ArrayList in this bundle?
FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();
TOnlineMapViewFragment mapfrag = TOnlineMapViewFragment.newInstance(b);
ft.replace(R.id.container, mapfrag).addToBackStack(null).commit();
}
}
So I've created the bundle and I wanted to pass it to the next fragment with the newInstance() method. How should I do this?
Consider implementing Parcelable interface in your classes. Then you would be able to store PlaceDetails in Bundle and pass it to setArguments() method.
I've found a tutorial: http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
I haven't testes it but it looks good.
Are you really sure a bundle is really needed?
TOnlineMapViewFragment mapfrag = TOnlineMapViewFragment.newInstance(b);
mapfrag.setPlaceList(place_list)
Should be trivial to create setPlaceList(ArrayList<PlaceDetails> place_list) ...