How to open another fragment from inside a ViewPager fragment? - android

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"

Related

android fragments - if i add two fragments on top of each other what is callback when top one is removed?

when i have two fragments that are stacked on top of each other in a framelayout using "add" transaction how can i know what the call back is when one is removed.
so imagine i have a framelayout like this:
<FrameLayout
android:id="#+id/fl_cart_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"/>
and then i add two fragments like this:
mFragment = new ExampleOneFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fl_cart_address, mFragment).commit();
mFragment = new ExampleTwoFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fl_cart_address, mFragment).commit();
now if i hit the back button then fragmentTwo is gone. but what call back can i get in FragmentOne so i know that its actually gone ? Basically i just need a callback when the top fragment gets popped off the backstack. I tried onResume but its not working.
use FragmentManager.OnBackStackChangedListener
Register a callback to using FragmentManager.addOnBackStackChangedListener(FragmentManager.OnBackStackChangedListener listener)
it will send you a callback when the Backstack is changing.

Android toolbar showing images of previously added fragment

I used toolbars in fragments instead of inflating it to activity.I am using different ImageViews in each toolbar layout.I was working fine. When I changed fragment transaction replace by add it is showing images of current layout images and previous fragment's images also.transaction.add(R.id.fragment_container, newFragment);What can I do?
I replaced replace by add since it showing some transparency while animating the second fragment from bottom.
This is how I added second fragment,
Fragment newFragment = new DetailsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_up, R.anim.splashfadeout);
transaction.add(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
if You use image different image in different fragment then you must hide or remove resourse onDestroy like it.
#Override
public void onDestroy() {
super.onDestroy();
MainActivity activity = (MainActivity) getActivity();
activity.mToolbarRightImageView.setVisibility(View.GONE);
}
if in which fragment you not use that ToolBarimage then on create of fragment you also hide that image.I faced this problem but i solved this way
MainActivity activity = (MainActivity) getActivity();
activity.mToolbarRightImageView.setVisibility(View.GONE);
If You want to use toolBar ImageView in each fragment , You make public in Mainactivty then you use above instruction

how can i handle the fragment1 onclick perform in fragment2?

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

Navigate back to activity from fragment

I have class Demo and I am extends the demo by FragmentActivity class. Also I have another class Fragment1 extends Fragment. And the onclick of button i am navigate from activity Demo to the Fragment Fragment1. Now I want to come back on Demo from the Fragment1. So how can I back to Demo activity?
Thanks.
Your question is lacking a lot of detail, so I'm taking a stab in the dark here, but...
I presume in your onClick code, you use the Fragment Manager to create a new Fragment Transaction, then add the fragment to that transaction and commit it?
Your problem with the activity being closed when you hit the back button is likely because your fragment was not added to something called the "back stack". You can find to documentation here http://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack%28java.lang.String%29, but crucially the main thing you need to do is modify your code to include the line below:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, new Fragment1());
fragmentTransaction.addToBackStack("Transaction ID"); // <-- This is key!
fragmentTransaction.commit();
Once that's done, Android will remember the adding of the fragment as a navigation act, and should reverse the transaction when the back button is hit.
If this wasn't what you're looking for, or if it doesn't work, please provide some more detail and some code samples and I can take another look.
Try adding the fragment you want to go back to manually to the backstack.
FragmentTransaction transaction = getFragmentManager().beginTransaction();
YourFragmentName myFragment = new YourFragmentName();
transaction.replace(R.id.fragment_container, myFragment);
//if you with to add it to backStack, do this, otherwise skip the line below
transaction.addToBackStack(null);
transaction.commit();

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.

Categories

Resources