my goal is to transport fragment data to another fragment.
this is what in have:
String ProductNummer_VALUE = NummerName.getText().toString();
Intent modify_intent = new Intent(getActivity(), MyActivity.class);
modify_intent.putExtra("memberName", ProductNummer_VALUE);
Log.i("MyActivity", "ListHistoryFragment " + ProductNummer_VALUE);
((MyActivity)getActivity()).test();
MyActivity:
public void test(){
// Create new fragment and transaction
Fragment newFragment = new DisplayFragment();
FragmentTransaction fragmentManager = getSupportFragmentManager().beginTransaction();
fragmentManager.replace(R.id.container, newFragment).addToBackStack(null).commit();
}
i tried using several methods, but i cant seem to get a handle on it.
Data is not being transported, because i tried using intents, to transport the data, but this way i couldnt launch to the new fragment. It just stayed on the MyActivity.class
try this,
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
In onCreateView of the new Fragment:
String value = getArguments().getString("YourKey");
Related
I want to pass from a fragment of the navigation drawer menu to a fragment outside of the menu. I manage to display the new fragment but overriden to the previous. Can you suggest me some link?
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction t1 = manager.beginTransaction();
DetailFragment instance = new DetailFragment();
Bundle b2 = new Bundle();
b2.putString("Title", Title);
b2.putString("author", author);
instance.setArguments(b2);
t1.replace(R.id.nav_host_fragment, instance);
t1.commit();
The problem is the R.id.nav_host_fragment
public void routine(String Title, String author,Fragment fragment,FragmentManager f1) {
FragmentTransaction t1 = f1.beginTransaction();
DetailFragment instance = new DetailFragment();
Bundle b2 = new Bundle();
b2.putString("Title", Title);
b2.putString("author", author);
instance.setArguments(b2);
t1.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
t1.replace(fragment.getId(), instance,"Change layout");
t1.commit();
}
The above function is recall by my first fragment in the code below to pass to the next fragment where the first fragment is come from nav bar fragment and the next fragment is an empty fragment
androidx.fragment.app.Fragment fragment =getParentFragment();
instance.routine(Title[position],Author[position],fragment,
instance.getSupportFragmentManager());
SectionFragment.class
Intent subjectDetail = new Intent(getContext(), GradeFragment.class);
ListGradeData clickItem = sectionList.get(position);
subjectDetail.putExtra(EXTRA_SECTION_ID, String.valueOf(clickItem.getSectionId()));
startActivity(subjectDetail);
GradeFragment.class
String studSectionId = getActivity().getIntent().getExtras().getString(EXTRA_SECTION_ID);
Toast.makeText(getContext(), studSectionId, Toast.LENGTH_LONG).show();
it doesn't display the studentSectionId. When I run the app it always crashed after I click the button
For fragment use FragmentManager to add/replace fragments. and create send data like
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("someInt", someInt);
myFragment.setArguments(args);
I am trying to send value as a parameter from one fragment to another using bundle but bundle is not is not putting in bundle. i tried to get value in next fragment but value was 0 then i debugged and got that value is not saving in bundle. screenshot is attached.
Here is code
switch(v.getId())
{
case R.id.store_gift_promo:
{
Fragment fragment = new GiftPromotion();
Bundle bundle = new Bundle();
bundle.putLong("id",id);
fragment.setArguments(bundle);
FragmentManager fragmentManager = ((FragmentActivity)mActivity).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
break;
}
}
Please help me solve the problem.
Try it in another way like this:
GiftPromotion gp = GiftPromotion.newInstance(id);
in GiftPromotion:
public static GiftPromotion newInstance(long id) {
GiftPromotion f = new GiftPromotion ();
Bundle args = new Bundle();
args.putLong("longValue", id);
f.setArguments(args);
return f;
}
while retrieving in onCreate of fragment:
long lId = getArguments().getLong("longValue", 0L);
In my Xamarin android app I have a two fragments A & B, A sends a value of string name to Fragment B like below,
mFragmentA = new FragmentA();
string name = "john";
var fragmenttransaction = FragmentManager.BeginTransaction();
fragmenttransaction.Replace(Resource.Id.FragmentContainer, mFragmentA, "mFragmentA");
fragmenttransaction.AddToBackStack(null);
fragmenttransaction.Commit();
Bundle bundle = new Bundle();
bundle.PutString("namekey", name);
mFragmentA.Arguments = bundle;
return mFragmentA;
In fragment B in the oncreateView I try to retrieve it as below,
Bundle bundle = this.Arguments;
var name= bundle.GetString("namekey", "Default Value"); //NULL REFERENCE EXCEPTION
Am getting a null reference exception on the name variable. Why? How do i get the string name in fragment A and assign it to name variable in Fragment B?
Cause you first replace fragments, and then put bundle inside a fragment, which is already on screen, here you are #jobbert xD
Change your code as below
mFragmentA = new FragmentA();
string name = "john";
var fragmenttransaction = FragmentManager.BeginTransaction();
Bundle bundle = new Bundle();
bundle.PutString("namekey", name);
mFragmentA.setArguments(bundle);
fragmenttransaction.Replace(Resource.Id.FragmentContainer, mFragmentA, "mFragmentA");
fragmenttransaction.AddToBackStack(null);
fragmenttransaction.Commit();
return mFragmentA;
Try this example. Before you commit fragment transaction. And after putting all your data to fragmentB object you should replace it with fragmentA.
Bundle bundle = new Bundle();
bundle.PutString("namekey", name);
fragmentB.setArguments(bundle);
Why not just use the constructor or properties?
string name = "john";
mFragmentA = new FragmentA(name);
var fragmenttransaction = FragmentManager.BeginTransaction();
fragmenttransaction.Replace(Resource.Id.FragmentContainer, mFragmentA, "mFragmentA");
fragmenttransaction.AddToBackStack(null);
fragmenttransaction.Commit();
return mFragmentA;
The constructor will execute before the OnCreateView and OnCreate methods execute.
NOTE: In your code, there is no FragmentB. So this is really just passing data from the current Activity/Fragment to FragmentA(since that's what's getting instantiated).
Hi I know there are answers of this question. I have tried all of them but it is not working in my app.
I am developing an app that has 3 Fragment activity. First fragment shows a website, second fragment has listview and third Fragment has another listview. Now I want to send URL from third fragment to first fragment when user clicks on listitem..This is what I have done.
I am sending string url from this Fragment to first fragment.
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
FragmentC fragment = new FragmentC();
final Bundle bundle = new Bundle();
bundle.putString("position", "http://www.facebook.com"); fragment.setArguments(bundle);}
});
This is first fragment where I need the url and Want to show in the webview.
String url="http://www.hotelsearcher.net/";
Bundle args = getArguments();
if (args != null){
url = args.getString("position");
}
WebView webView= (WebView) V.findViewById(R.id.webView1);
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setPluginState(PluginState.ON);
webView.loadUrl(url);
When I click on list item I don't see anything . It does not redirect me to the first fragment.Please help me..
Use Bundle to send String:
//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "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");
1.If fragments are hosted by same activity- You cannot cast an intent to Fragment. Fragment acts as a part of Activity, it is not an activity by itself. So to share a string between fragments you can declare a static String in Activity. Access that string from Fragment A to set the value and Get the string value in fragment B.
2.Both fragments are hosted by different Activities- Then you can use putExtra to pass a string from Fragment A of Activity A to Activity B. Store that string in Activity B and use it in Fragment B.
You have to attach your bundle to your fragment.
fragment.setArguments(bundle);
and after that change or replace the new fragment.
You have to be sure that the String is in the new Fragment. Debugging!!
You can send data by two ways
First when you want to start that fragment while sending data
SecondFragment ldf = new SecondFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
Second, when you want to send data without open your fragment
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.add(R.id.fragContainer1, new ModelListFragment(), FRAG_MODEL_LIST);
ft.add(R.id.fragContainer2, new TrimListFragment(), FRAG_TRIM_LIST);
ft.commit();
Fragment fragment = mFragmentManager.findFragmentByTag(MainActivity.FRAG_MODEL_LIST);
Log.d("MY", "found fragment: " + (fragment != null));