I have two fragments and I am trying to pass an integer between them. In the first one, in Fragment_1.java I have this in onCreate:
pontszam = 12;
Fragment fragment = new Fragment_2();
Bundle bundle = new Bundle();
bundle.putInt("pont", pontszam);
fragment.setArguments(bundle);
Then in the second fragment, in Fragment_2.java I have this in onCreate:
Bundle bundle = this.getArguments();
int myInt = bundle.getInt("pont");
TextView pontjelzo = (TextView)rootView.findViewById(R.id.pontszam_2);
pontjelzo.setText(Integer.toString(myInt));
But I get nullpointer exception on this line:
int myInt = bundle.getInt("pont");
What am I doing wrong?
And this is how I open my second fragment:
button_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left).replace(R.id.content_frame, new Fragment_2());
fragmentTransaction.commit ();
}});
You are replacing your first Fragment with a new Fragment_2 instead of the one which has a Bundle.
Declare your Fragment_2above onCreate (globally), instantiate it:
fragment = new Fragment_2();
And change your replace line:
fragmentTransaction.replace(R.id.content_frame, fragment);
Related
i have being trying to solve this problem for while thought, i'm trying to pass the data from Fragment to other Fragment, the model.getPid suggesting me to be static!, and after running the app and clicking on the image then the app crachs!
thanks in advance!
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ProductsDetailsFragment fragment = new ProductsDetailsFragment();
Bundle b1 = new Bundle();
b1.putString("pid", Products.getPid());
fragment.setArguments(b1);
FragmentManager fragmentManager =
getActivity().getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.nav_host_fragment, fragment)
.addToBackStack(null)
.commit();
this is from the second Fragment
Bundle bundle = this.getArguments();
if (bundle != null) {
productID= bundle.getString("pid");
getProductDetails(productID);
}
in the FirebaseAdapter when i pass the ProductTD, it returns null
private void getProductDetails(String productID) {
DatabaseReference productRef = FirebaseDatabase.getInstance().getReference().child("Products");
productRef.child(productID).addValueEventListener(new ValueEventListener() {
#Override
this is from Logcat
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
here is your error Intent intent = new Intent(getActivity(), ProductsDetailsFragment.class);
intent.putExtra("pid", model.getPid());
startActivity(intent)
remove this you can use intent for fragment
I am developing an app in which I have used bottom navigation bar so for that I had to use fragments. In my fragments, I implemented Recycler view. So My question is when I click on an item of recycler view, how can I navigate to another fragment (which is in a different item of the bottom navigation bar) and how can I transfer the data between two fragments. Please Help.
during onClick() pass data through bundle
like that
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
Bundle bundleobj = new Bundle();
bundleobj.putCharSequence("key", data);
Fragment2 fragobj = new Fragment2();
fragobj.setArguments(bundleobj);
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.replace(R.id.containerView, fragobj).commit();
In Fragment2 class:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle =getArguments();
if(null!=bundle) {
myData=bundle.getCharSequence("key");
}
}
In first Fragment..
Fragment fragment = new EditExamFragment();
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
Bundle bundle = new Bundle();
bundle.putString("branch_id", mDataset.get(position).getiBranchId());
bundle.putString("exam_id",mDataset.get(position).getiExamId());
fragment.setArguments(bundle);
In Second Fragment for fetching values try below code;
String branch_id, exam_id ;
final Bundle bundle = this.getArguments();
if (bundle != null) {
branch_id = bundle.getString("branch_id");
exam_id = bundle.getString("exam_id");
}
Add below code after event listener in fragment1
SecondFragmentName secondFragmentName = new SecondFragmentName();
Bundle args = new Bundle();
args.putString("key", "value");
secondFragmentName.setArguments(args);
getFragmentManager().beginTransaction().replace(R.id.content_frame, secondFragmentName).addToBackStack("Some string").commit();
To get value in Fragment2
String message = getArguments().getString("key");
I want to make 2 fragment, if I press the question then will replace the fragment question, and if the result of the fragment results. but of these codes are replaced only fp.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tersumbat);
strings = getResources().getStringArray(R.array.Tersumbat);
hasilstrings = getResources().getStringArray(R.array.HasilTersumbat);
FragmentManager fm = getSupportFragmentManager();
Tersumbat.FragmentPertanyaan fp = new Tersumbat.FragmentPertanyaan();
Tersumbat.FragmentPertanyaan fh = new Tersumbat.Fragmenthasil();
fp.setListener(this);
Bundle bundle = new Bundle();
bundle.putString("pertanyaan", strings[0]);
bundle.putString("hasil", strings[0]);
bundle.putInt("posisi", 0);
fp.setArguments(bundle);
fh.setArguments(bundle);
fm.beginTransaction().replace(R.id.content_frame, fp).commit();
fm.beginTransaction().replace(R.id.content_frame, fh).commit();
}
Try this code
fragname fragname = new fragname();
Bundle bundle = new Bundle();
bundle.putCharSequence(bundle_string, yourstring);
fragname.setArguments(bundle);
FragmentManager manager = getActivity().getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragmentlayout, fragname, fragname.getTag()).commit();
Note:
if you're in a Activity and you're calling a new fragment, take out the IgetActivity()., its only necessary whet you call a fragment from another fragment
I am trying to pass an object Fragment -> Activity -> Fragment.
My 2nd Fragment says my object is null when getting arguments:
Fragment A: (Sending Object, communicator is interface to Main Activity)
meetsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Meet meet = new Meet();
meet = meetList.get(position); // meet is not null here
communicator.changeToolbarTitle(meet.getName());
****** Sends Meet object to main activity ********
communicator.sendMeetToMeetProfile(meet);
}
});
Main Activity:
#Override
public void sendMeetToMeetProfile(Meet meet) {
MeetAthletesListContainer meetAthletesListContainer = new MeetAthletesListContainer();
***** Fragment is not replaced just called *******
MeetAthletesMales meetAthletesMales = new MeetAthletesMales();
MeetAthletesFemales meetAthletesFemales = new MeetAthletesFemales();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Bundle bundleMales = new Bundle();
bundleMales.putParcelable("meetAthletesMales", meet);
meetAthletesMales.setArguments(bundleMales);
Bundle bundleFemales = new Bundle();
bundleFemales.putParcelable("meetAthletesFemales", meet);
meetAthletesFemales.setArguments(bundleFemales);
******* Replaces current fragment with container that contains above (2) fragments *******
transaction.replace(R.id.frameLayout, meetAthletesListContainer, "meetAthletesListContainer");
transaction.addToBackStack(null);
transaction.commit();
}
Fragment B: (Fragment is contained inside another fragment)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_meet_athletes_males, container, false);
Log.e("", "Fragment onCreateView Called!");
initializeVar();
*****Says this meet object is null*****
meet = getArguments().getParcelable("meetAthletesMales");
}
I am replacing a fragment with another but passing the object to a fragment which is inside another fragment(meet_container_fragment). So my object is being sent directly to the fragment. I got this working in another instance where I actually replace the fragment but I want to replace the FRAGMENT CONTAINER, not the actual fragment B
Try this
MeetAthletesListContainer meetAthletesListContainer = new MeetAthletesListContainer();
***** Fragment is not replaced just called *******
MeetAthletesMales meetAthletesMales = new MeetAthletesMales();
MeetAthletesFemales meetAthletesFemales = new MeetAthletesFemales();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putParcelable("meetAthletesMales", meet);
bundle.putParcelable("meetAthletesFemales", meet);
meetAthletesFemales.setArguments(bundle);
******* Replaces current fragment with container that contains above (2) fragments *******
transaction.replace(R.id.frameLayout, meetAthletesListContainer, "meetAthletesListContainer");
transaction.addToBackStack(null);
transaction.commit();
Try this,
main activity
FragmentB fragmentb = FragmentB.newInstance(appContext,meet);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.contentContainer, fragmentb , tag);
Fragment B
public static FragmentB newInstance(AppContext appContext, Meet meet) {
FragmentB fragment = new FragmentB();
Bundle args = new Bundle();
args.putSerializable(ARG_APP_CONTEXT, appContext);
args.putString(ARG_MEET_NAME, meet.name);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
appContext = (AppContext) getArguments().get(ARG_APP_CONTEXT);
name= getArguments().getString(ARG_MEET_NAME);
}
}
also make Meet implements Serializable
I have trouble understanding in FragmentTransaction.replace(id, fragment, 'string') what does .replace actually
I read somewhere that it will replace the content of view id with fragment but in my
public class MainActivity extends
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container) !=null) {
if(savedInstanceState!=null){
return ;
}
ArticleFragment first_fragment = new ArticleFragment();
HeadlineFragment second_fragment = new HeadlineFragment();
first_fragment.setArguments(getIntent().getExtras());
second_fragment.setArguments(getIntent().getExtras());
FragmentTransaction manager=getSupportFragmentManager().beginTransaction();
manager.add(R.id.fragment_container, first_fragment,"first");
manager.add(R.id.fragment_container, second_fragment,"second");
manager.commit();
}
abc newFragment = new abc();
Bundle args = new Bundle();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment,"second");
transaction.addToBackStack(null);
transaction.commit();
}
}
it only replaces the first fragment that i added to Transaction, i.e. first_fragment.
And how do i replace a specific fragment?
The issue seems to be with R.id.fragment_container. You should have a different first argument on the line below in order to replace a fragment that is not your "first" fragment:
manager.add(R.id.fragment_container, second_fragment,"second");