Move to one fragment to another fragment of different container activity - android

I have an container activity which will contain the fragments which will be added to this container when my bottom nav bar is navigating. Now I want to go to one of this fragment to another fragment which will replace another different activity by set on click listener of a cardview.
I am new fragment so I dont understand how to do that. Hope someone will help me.

You approach is wrong. You cannot navigate from one activity fragment to another activity fragment. The correct way will be to have single activity with multiple fragments inside. Using Navigation component will be the best way of navigation.

Related

Understanding android navigation drawer and fragments

i'm quite new to drawer material and i have trouble understanding some things:
I need to create an Activity with a Fragment on it. Different selections on the Drawer must replace the current Fragment with another one, but is the Drawer something in the fragment or it is one for the activity itself. More specifically is the Drawer living in the Fragment and if not is it possible to create it in the Fragment. I am asking this cause i see that when initiating the Drawer you need to fill parent activity. Also when i tried to use the Navigation Drawer template in Android Studio i didn't had the Use a Fragment checkbox.
There are many ways to achieve what you want. But I think the simplest way is:
1) DrawerLayout view should reside in the activity (probably as the base layout).
2) When you click on an item in the draw 2 things happen:
The fragment is replaced (you have one layout to contain the fragment and you just replace the fragment in it).
The items inside the drawer update (if you are making a list you would simply set the data and call notifyDataSetChanged().
Don't forget to save your state so it can recover in case the Activity is recreated.

Android - how to slide screens, inside a fragment

I've created an app, that has a main activity with a drawer menu so the user can click on some option and a fragment is open inside the activity.
What I'd like to do is to have several screens in one of the options and to navigate between them by tabs/slide the screen.
I saw an example of doing it inside one activity and with a fragment per 'sub-screen', my question is: how can I do it when I'm already 'inside' a fragment?
Thanks
Fragments can support having other Fragments inside them. The key to making this work is to remember to use getChildFragmentManager() to get the Fragment's FragmentManager instead of getFragmentManager() which gets the Activity's FragmentManager.
If you want to swipe between views, use a ViewPager inside your Fragment UI. The ViewPager will use a FragmentPagerAdapter to handle the Fragments for display.

Multiple fragments or nested fragments

I am writing an android app that will have a number of different screens that I would like to swipe between, each screen will be a full page except for action bar header. On each screen there is the ability to open up another screen which will also be multiple screens that I would like swipeable. What is the best way to handle this. Do I have one fragment manager that holds all the screens and handle the onPageScrollStateChanged to only allow swipes between the current accessable screens or would I be best off nesting the fragments. I hope the above makes sense.
Thanks in advance
Sounds like you want to use a ViewPager to to swipe between views (Fragment extends View)
You could either:
In a single activity use a FragmentManager that switches between the parent and child Fragments, each with their own ViewPager and nested Fragments
Start a new activity to hold each ViewPager
Both are valid, if the Fragments need to communicate with each other or the Activity option one might suit the project needs better.
For the swiping between views you indeed need a ViewPager
For the nested fragments I would use a wrapper. I struggled a lot with fragments and found that this is the best way. A wrapper is very simple. This is just a fragment that holds other fragments. In the onCreate() of this fragment you get the childFragmentManager and add the fragment you originionally wanted to add. If you want to go to a new fragment you simply get the childFragmentManager again and replace the current item. This way you have nested fragment. You can add this to the backstack in order to get back navigation, but you need to override onBackPressed() inside your activity and call the method popBackStack() from the fragmentManager in order to get the first fragment back.
If you have any questions, comment below.

fragment inside a fragment in android

In my application I am making use of navigation drawer which is in the MainActivity and this navigation drawer has say 5 fragments. I am not maintaining any backstack of these fragments.
Now, the first fragment has one button which when clicked pushes a fragment (which I call an inner fragment). Here, I am maintaining a backstack because I want to get back to the first fragment from the inner fragment.
Now, I have a requirement in which I want to navigate from an activityA to the inner fragment.
Is this possible?
One way that I have thought of is to have the push code inside the first fragments create method (and make this conditional).
But I don't think its an appropriate way. Any suggestions would be helpful.

How to call an activity inside a fragment

I have created a fragment inside an Activity, and now I want to open another application/Activity inside this fragment, Result that I want is, both the activities should be seen on the display (it should not open in another window). Please let me know how can I achieve this.?
Nope you cannot achieve this, you cannot start an activity inside a fragment nor you can show a fragment inside a fragment. For showing multiple layouts in a single screen or activity you need to adjust your activity's layout file and arrange different fragments accordingly ...

Categories

Resources