Data passing to a Fragment - android

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();

Related

Declared target fragment that does not belong to this FragmentManager

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.

Return to appropriate Fragment from called Fragment

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 remove ViewPager from current fragment when called another fragment

When I called another fragment, ViewPager of first fragment is not removed. I have spent 2 days on it but didn't get it. I will be very thankful if some one will help me.
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment=new SecondPage(urlToSecondPageStory,storyCategoryContainer.get(position));
Bundle bundle = new Bundle();
bundle.putString("imageurl",imageUrl);
fragment.setArguments(bundle);
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(android.R.id.content,fragment);
// transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//transaction.addToBackStack(null);
transaction.commit();

hide existing fragment and load new one on same frame

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;
}

Android start Fragment putString

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.

Categories

Resources