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.
Related
I have three fragments a, b, c
I replace a with main activity
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fram_container, new Fragment1(), "tag1");
transaction.commit();
a to b
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fram_container, new Fragment2(), "tag2");
transaction.commit();
and b to c
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fram_container, new Fragment3(), "tag3");
transaction.commit();
in c...
I want c back pressed to redirect with a without anything doing in c
I want skip b in back pressed.
What can I do and how?
Implement the below code back press
public void backToFragment(final Fragment fragment) {
// go back to something that was added to the backstack
getSupportFragmentManager().popBackStackImmediate(
fragment.getClass().getName(), 0);
// use 0 or the below constant as flag parameter
// FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
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);
How to refresh or rebuild a class that is extended from RowsFragment?
I have tried a few codes:
RowsFragment frg = this;
frg.getFragmentManager();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
or
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
but still nothing.
You are simply using same instance while reattaching the fragment.
Create new instance of the fragment and replace it with existing one.
RowsFragment fragment = new RowsFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment); // use your container in the place of R.id.container
fragmentTransaction.commit();
I use the one frame on which changing the fragment views, the problem I have is when I place add instead of replace the new view is placed on the existing one
all I need is it has to replace the existing and place the new one
As I have to reuse the current view when back button is pressed cannot use the replace
Fragment fragment = null;
fragment = new DetailFragment2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container2, fragment)
.addToBackStack(null).commit();
If you don't use the support library (which is your actual case), use this code:
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
If one day you'll decide to use the support library, then use this instead (it's only a matter of replacing getFragmentManager() with getSupportFragmentManager()):
final FragmentTransaction ft =
getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction;
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_container2,fragment.newIstance());
transaction.addToBackStack(null);
transaction.commit();
And you need to create newIstance in Fragment class:
public static fragment newIstance(){
fragment frag= new fragment();
return frag;
}
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();