how can i handle the fragment1 onclick perform in fragment2? - android

I am develop the app using the fragments and i am facing one fragment1 adding to the another fragment2
fragment1 onclicks performing fragment2 .I didn't findout the solution for that. please guide anyone know
I am using the following code to add the fragment
Fragment2 fragment2=new Fragment2();
FragmentManager fragmentManager = activity.getFragmentManager();
android.app.FragmentTransaction ft=fragmentManager.beginTransaction();
ft.add(R.id.container,fragment2);
ft.hide(new Fragment1());
ft.addToBackStack(Fragment1.class.getName());
ft.commit();
The above to use adding onefragment1 adding to fragment2.Fragment1 onclicks perform to fragment2 . i didn't findout mistake.
please guide me anyone know .Advance thanks to all

nested Fragments are not recommended and for adding a fragment to a container from another Fragment use the parent Activity to do so , you can define a function inside Parent Activity which replace current Fragment with your second Fragment and call it from your first.

Adding a fragment to another fragment is the concept of nested fragments and not recommended. You should replace the fragment instead of adding. Use the following:
Fragment fragment = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().addToBackStack("fragment").replace(R.id.frame_container, fragment).commit();

Related

How to open another fragment from inside a ViewPager fragment?

I followed this tutorial to build an app with three fragments in sliding view (ViewPager).
https://c1ctech.com/android-viewpager-example-to-create-sliding-screens/
The app was built successfully. Then I added a button inside FragmentOne. And also created another fragment named FragmentFour with fragment_four.xml layout file.
Now I want to link the FragmentFour with FragmentOne through onClickListener with the button I created before. When in FragmentOne the button will be clicked, and it'll launch FragmentFour. To achive that, inside onClickListener I added these codes-
FragmentFour fragFour = new FragmentFour();
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_frameLayout, fragFour);
transaction.addToBackStack(null);
transaction.commit();
But after running the app, when I click on that button from inside the FragmentOne, FragmentFour's layout overlaps over the FragmentOne's layout. And FragmentTwo and FragmentThree can be accessible via sliding from FragmentFour, but I don't want this.
If anything wrong here, please help me.
Your code is replacing FragmentOne with FragmentFour in the ViewPager:
transaction.replace(R.id.fragment_one, fragFour);
which results in a ViewPager with FragmentFour, FragmentTwo, and FragmentThree
It seems like you want to start a new Activity for FragmentFour. From your onClick handler in FragmentOne, start a new activity as follows:
getActivity().startActivity(new Intent(context, FragmentFourActivity.class));
FragmentFourActivity is an Android Activity (e.g. AppCompatActivity) whose layout contains FragmentFour. Follow along at https://developer.android.com/training/basics/firstapp/starting-activity if unsure how to do so.
The problem is now gone. I just need to make few changes to my codes. Inside onClickListener, I changed my codes like this-
Before:
FragmentManager fragmentManager = getChildFragmentManager();
After:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
And inside the fragment_four.xml file's relative layout:
android:background="#color/bgcolor"
android:clickable="true"
android:focusable="true"

Fragment with MapView is never removed from FragmentManager

I have a Fragment with MapView in it. I add Fragment to container with following code:
Fragment fragment = new MyLocationFragment()
String tag= fragment.getClass().getName();
FragmentTransaction transaction = fm.beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
R.anim.slide_in_left, R.anim.slide_out_right);
transaction.replace(R.id.container, fragment, tag);
transaction.addToBackStack(null);
transaction.commit();
Now when user presses back, this Fragment is supposed to be removed from FragmentManager. But if execute this code :
Fragment fragment = getSupportFragmentManager().findFragmentByTag(MyLocationFragment.class.getName());
It never returns null, even if I replace other fragments in same container. What can I do to remove Fragment from FragmentManager? Am I doing something wrong here ?
When an activity is destroyed, its FragmentManagersaves out its list of fragments. When the activity is recreated, the new FragmentManager retrieves the list and recreates the listed fragments to make everything as it was before.
[Refer to book "Android Programming The Big Nerd Ranch Guide" P150]
So your should remove fragment from fragmentmanager.
Refer to this Q&A Remove old Fragment from fragment manager
Your question maybe duplicate with thisHow to get a Fragment to remove itself, i.e. its equivalent of finish()?

Use Viewpager with NavigationDrawer leads to Blank page

When click the items of NavigationDrawer,The main activity container switch the fragments to show,It worked fine until I met this:
1.Switched to FragmentA, which CONTAINS A VIEWPAGER,it showed well.
2.Switched to fragmentB,fragmentB showed well.
3.Swiched back to
FragmentA,it shows as a Blank View
I tried to flip horizontally on it,I can see the viewpager index do changed(in log),But I don't know why it showed as a blank page.
*and,if fragmentA dose not contains viewpager,it worked well
Any suggestion would be appreciated.
I use replace() to switch between the fragments:
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, currentFragment)
.commit();
Possible reason might be not using replace transaction instead you mightbe adding fragments and adding it to backstack. Try using replace transaction, and see if it works.
Fragment1 firstFragment = new Fragment1();
Bundle bundle = new Bundle();
firstFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.article_fragment, firstFragment)
.commit();
Learn More on Fragments adding to back stack:
Android Fragment transaction: FragmentManager and Backstack

new fragment squeezing old fragment on add method

I um uzing this code:
FragmentManager FManager = getFragmentManager();
FragmentTransaction FTransaction = FManager.beginTransaction();
FTransaction.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragment.setArguments(fragment.getArguments());
FTransaction.add(ResourceId, fragment, label);
FTransaction.addToBackStack(backStack);
FTransaction.commit();
and I m geting this on my activity:
http://i.stack.imgur.com/K9NKy.png
can someone help me?
I hope that my simple answer will help you. Currently, You are loading an another fragment over your main fragment. you need to "hide" or "remove" or "replace" with old one. As i mentioned them below
In case of Remove or Hide
Fragment fr2 = fragmentManger.findFragmentByTag(Constants.TAG_Detail_Page);
if (fr2 != null)
fragmentTransaction.remove(fr2);
While for "Replace", you will assign new fragment to contentView:
FTransaction .replace(R.id.fragment_container, newFragment);
You are asking the fragment manager to add another fragment and it is doing just that. You never tell it to do anything to the original fragment. If you want to remove the exisitng fragment and show a new one then you should be calling:
FTransaction.replace(...)
instead of calling add.
If the original fragment has been added via the xml layout file, it is more difficult to replace. There are multiple SO questions dealing with that situation.

Replacing one fragment with other in viewpager

I'm using a ViewPager together with a FragmentPagerAdapter to host four different fragments.
What I'm trying to achieve is to successfully replace Fragment1 with a whole new fragment, newFragment,on click of a button.
When I use..
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment1_layout_id, newfragment);
transaction.remove(fragment1);
transaction.commit();
The fragment is replaced and Fragment is shown instead of Fragment1. Though as soon as I swipe all the way to Fragment4 and then back to newFragment, Fragment1 has made a comeback.
Your fragments are provided by FragmentPagerAdapter.
You would need to adapt what fragment your FragmentPagerAdapter creates for your Fragment1 and then call notifyDataSetChanged() on the adapter.
With your approach, the time Fragment1 should be shown again, the ViewPager will simply ask your FragmentPagerAdapter to re-create the specific instance - which will give you back the instance you were trying to replace.
Not that ViewPager can cache fragments internally, as long as notifyDataSetChanged() was not called on the adapter. Hence, it may not pick up chances you have made without that call.

Categories

Resources