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);
Related
I have an Activity with a container being a FrameLayout. I need to add a fragment to that container, but doing so throws an IllegalStateException, destroying my Activity.
This is my container in the activity layout:
<FrameLayout
android:id="#+id/seeMoreContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
This is how I add the fragment to the activity
PlacesSeeMoreFragment placesSeeMoreFragment = new PlacesSeeMoreFragment();
Bundle bundle = new Bundle();
bundle.putInt(PlacesSeeMoreFragment.KEY, poiID);
placesSeeMoreFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.seeMoreContainer,
placesSeeMoreFragment).commit();
And this is the logcat
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
at net.ilb.Activities.PlacesActivity.openPlaceSeeMore(PlacesActivity.java:140)
What am I doing wrong here?
EDIT:
I add the fragment to the activity from a public method, which I call from onItemClickListener of a ListView
This is the whole method
public void openPlaceSeeMore(int poiID){
PlacesSeeMoreFragment placesSeeMoreFragment = new PlacesSeeMoreFragment();
Bundle bundle = new Bundle();
bundle.putInt(PlacesSeeMoreFragment.KEY, poiID);
placesSeeMoreFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.seeMoreContainer, placesSeeMoreFragment).commit();
}
And this is how the method is called
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
PlacesActivity placesActivity = new PlacesActivity();
placesActivity.openPlaceSeeMore(poi.getId());
}
});
I guess you are calling this:
PlacesActivity placesActivity = new PlacesActivity();
This is a very wrong approach to call a method declared in activity.
Which is creating new instance of your activity every time you click something on the list. Plus if you want to capture list clicks you should use an interface and not use activity objects anywhere.
Hope this helps.
Use below code....
Fragment fragment = new PlacesSeeMoreFragment();
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
Bundle bundle = new Bundle();
bundle.putInt(PlacesSeeMoreFragment.KEY, poiID);
fragment.setArguments(bundle);
Sometime I faced the same issue and this is what I did. Use commitAllowingStateLoss() instead of commit(). Try this one
getSupportFragmentManager().beginTransaction().add(R.id.seeMoreContainer, placesSeeMoreFragment).commitAllowingStateLoss();
try this ,
PlacesSeeMoreFragment placesSeeMoreFragment = new PlacesSeeMoreFragment();
Bundle bundle = new Bundle();
bundle.putInt(PlacesSeeMoreFragment.KEY, poiID);
placesSeeMoreFragment.setArguments(bundle);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.seeMoreContainer, placesSeeMoreFragment );
transaction.commit();
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).
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");
I use a master/detail flow and in every fragment I have a viewpager containing some forms but the thing is when I click on some other links from the master list, the content of the viewpager is lost. How to save this content so everytime I click on the same choice I find my data.
My code :
#Override
public void onItemSelected(String id) {
if (mTwoPane) {
// In two-pane mode, show the detail view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
if(id.contains("1"))
{
Bundle arguments = new Bundle();
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setRetainInstance(true);
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment).commit();
}
else if(id.contains("2"))
{
Bundle arguments = new Bundle();
ItemDetailFragment2 fragment = new ItemDetailFragment2();
fragment.setRetainInstance(true);
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment).commit();
}
} else {
// In single-pane mode, simply start the detail activity
// for the selected item ID.
Intent detailIntent = new Intent(this, ItemDetailActivity.class);
detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
}
}
EDIT:
I tried to modify my code this way :
if (id.contains("1")) {
Fragment currFragment = fm.findFragmentByTag(lastTag);
ItemDetailFragment newFragment = (ItemDetailFragment) fm.findFragmentByTag("f"+1);
FragmentTransaction ft = fm.beginTransaction();
ft.hide(currFragment);
if(newFragment==null)
{lastTag="f"+1;
newFragment=new ItemDetailFragment();
ft
.replace(R.id.item_detail_container, newFragment);}
else {
ft.show(newFragment);
}
ft.commit();
}
But i got a NullPointerException at android.support.v4.app.BackStackRecord.run(BackStackrecord.java:656)
Try to call setRetainInstance(true) in your fragments onCreate()
As writed here, if you do not add the transaction to the back stack, then the fragment is destroyed when removed or replaced.
You can hide current fragment, instead replacing, then add new. Like that:
int currPage = 0;
public void goToPage(int num) {
FragmentManager fm = getSupportFragmentManager();
Fragment currFragment = fm.findFragmentByTag("f" + currPage);
Fragment newFragment = fm.findFragmentByTag("f" + num);
FragmentTransition ft = fm.beginTransaction();
ft.hide(currFragment);
if(newFragment == null) {
// First use. Create new instance for page.
newFragment = new MyFragment();
ft.add(R.id.item_detail_container, newFragment, "f" + num);
} else {
// Fragment for page already added. Just show it.
ft.show(newFragment);
}
ft.commit();
this.currPage = num;
}
And don't forget to specify tag for initial page fragment too.
You can implement the onPause() or onStop() methods of your fragments and save the data when that methods are called, which happens automatically when they are removed.
Or you could perhaps go with the savedInstanceState.
It's better to save in the arrayList. Onclick store the image id in the arrayList.
Here is my code that is not working:
// Sending bundle this way:
String topUser = String.valueOf(scores.get(arg2));
Bundle data = new Bundle();
data.putString("userprofile", topUser);
FragmentTransaction t = getActivity().getSupportFragmentManager()
.beginTransaction();
SherlockListFragment mFrag = new ProfileFragment();
mFrag.setArguments(data);
t.replace(R.id.main_frag, mFrag);
t.commit();
// Retrieving this way:
Bundle extras = getActivity().getIntent().getExtras();
userName = extras.getString("userprofile");
Basically, the data isn't received. Am I on the right track or is there a better way of doing this?
You should be using the getArguments() method of the Fragment class. So put something like the following inside your Fragment:
Bundle extras = getArguments();
Reference: http://developer.android.com/reference/android/app/Fragment.html#getArguments()