i wrote code withc can to start new fragment.this is a source
Fragmen1 newFragment = new Fragmen1 ();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
not i want to use putExtra to put some String and show in my Fragmen1 fragment
P.s
in activity i know how to use putExtra,but fragment i never use it
Try something like this to pass a bundle:
Fragmen1 newFragment = new Fragmen1();
Bundle bundle = new Bundle();
bundle.putString("my_string","fragmentQ");
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
newFragment.setArguments(bundle);
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
To receive the bundle:
String str = getArguments().getString("my_string");
It is always better to check if getArguments() is null first.
Related
I used the following method to refresh Fergmant
Fragment frg = null;
frg = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_container);
final FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
Or
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
But after being updated
From
implementation 'androidx.appcompat:appcompat:1.2.0'
To
implementation 'androidx.appcompat:appcompat:1.3.0'
This methods no longer works. What method should I use to refresh the fragment now?
Maybe you can try different usage for fragment refresh:
YourFragment f2 = new YourFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, f2);
transaction.addToBackStack(null);
transaction.commit();
Yes, the methods you have used are deprecated. You can try this way using the function replace() which replace an existing fragment that was added to a container.
Fragment myFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, myFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
I've got a simple setup:
Code:
ActivityHaupt -> FragmentHaupt:
final FragmentHaupt fragment = new FragmentHaupt();
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frameLHaupt, fragment, null);
transaction.disallowAddToBackStack();
transaction.commit();
FragmentHaupt -> FragmentViewP:
FragmentViewPfragment2 = new FragmentViewP();
fragment2.setTargetFragment(FragmentHaupt.this, 12); //AppConstant.FRAGMENT_CODE = 12
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.frameLHaupt, fragment2);
fragmentTransaction.commit();
FragmentViewP-> FragmentAddDriver:
FragmentAddDriver ldf = new FragmentAddDriver ();
Bundle args = new Bundle();
args.putString("firstname", vName.getText().toString());
args.putString("lastname", nName.getText().toString());
args.putString("drivername", dName.getText().toString());
ldf.setArguments(args);
ldf.setTargetFragment(FragmentHaupt.newInstance("firstP","lastP"), 12);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.frameLHaupt, ldf);
fragmentTransaction.commit();
When I try opening the FragmentAddDriver activity, I receive the error in the title. What can the problem be? I am using always the same FragmentManager (getActivity().getSupportFragmentManager)?
What am I missing?
With the code here:
ldf.setTargetFragment(FragmentHaupt.newInstance("firstP","lastP"), 12);
You're not actually going back to the FragmentHaupt. You're adding a new instance of it.
To actually go back you've to set tag when you add it in MainActivity
transaction.add(R.id.frameLHaupt, fragment, "FragmentHaupt");
And inside FragmentViewP you've to find it using that tag.
ldf.setTargetFragment(fragmentManager.findFragmentByTag("FragmentHaupt"), 12);
This way you'll be able to go back to that instance.
So suppose there are 3 fragments, let's name them F1, F2 and F3. F3 can be called from both F1 and F2 by the following code:
Fragment f3 = new F3();
Bundle args = new Bundle();
args.putString("Id", String.valueOf(id));
f3.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.f1_layout, f3).commit();
I wanna return to where I come from. What can be done? I've played with addToBackStack in different ways but couldn't succeed.
This is how you use addToBackStack() method :
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(..............);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Try this
fragmentTransaction.replace(R.id.f1_layout,f3).commit().addToBackStack(null);
Fragment fragment = new Videonew_fragment();
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
using above code for go to another fragment.i need to pass data along with this method.for data passing i used below code it not working perfectly.what i am doing wrong? help me?
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
I'm not sure of the layout of your code, but you may be missing that you need to add the bundle to the specific fragment as you open it.
//First Create your arguments to pass
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
//Create New Fragment
Fragment fragment = new Videonew_fragment();
//Apply arguments
fragment.setArguments(bundle);
//Now create replace the fragment
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
I want to make two fragment transactions in a row.
Fragment1 fragment1 = new Fragment1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment1);
transaction.addToBackStack(null);
transaction.commit();
Fragment2 fragment2 = new Fragment2();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment2);
transaction.addToBackStack(null);
transaction.commit();
However, if I do so, there will be some concurrence problem.
Is there a way for me to implement a callback function so that I can start the second transaction right after the first one is finished?
You can manually flush the queue causing the pending fragment transaction to be executed. The call you're looking for is:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(android.R.id.content, fragment, name).addToBackStack(null).commit();
fm.executePendingTransactions();
By calling the fm.executePendingTransactions(); you force all pending actions to occur before continuing.