Getting viewpager in another activity and setting viewpager position in that activity - android

I implemented common navigation drawer for all activities, and I implemented that.But now my problem is I have fragments as well as activities in my Navigation drawer,
I set
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
for all activities to restrict inflating same activity for twice but now I am unable to do the same for fragments of same Activity. If I add the above flag for fragments main activity other fragments are not getting selected how to solve these.
Thanks in Advance.

Related

Android navigation with activity and PagerView2

I have an issue with my Android app that uses Navigation and ViewPager2.
I've the main Activity which implements a Navigation Drawer and has its own Navigation Graph and it works fine with the rest of the application.
Then I have a second Activity that has a ViewPager2 which displays 3 different Fragments. Now the problem is that I need to navigate from the Main Activity to the Pager activity keeping the original Navigation infrastructure. I know that each activity has it's own Navigation Graph, but the second activity has a ViewPager2 control so that means it has 3 Fragments and I cannot specify a startDestination in the new Navigation Graph .... because there are 3 and here is the problem. I can display the new activity but the Application Bar with the Back button which would navigate to the original activity is not displayed. Any solution ?
You can not handle fragment of viewpager through nav graph, but you can create parent fragment called ViewPagerTabsHolderFragment and inside it, you will set up the view pager adapter fragments then you can set the nav graph start destination to this ViewPagerTabsHolderFragment

Create a Navigation drawer to link to fragments and activities

So i have a navigation drawer like so it has 3 buttons that go to 2 different fragments and the other button which goes to an Activity.
When I click one frag 1 my fragment opens up with the drawer still intact same goes for Frag 2
but when i click on the Activity 1 the Drawer disappears
but i would like the drawer to continue in the activity as well.
can this be done.
What you'd want to do (roughly) is
Establish a menu as one of your resources and establish the items in that list.
In the activity that you want to contain the drawer, create
the drawer object and create a callback for the onMenuItemSelected.
In that callback, reference the menu item ids you created before and use intents and fragment managers to either start the activity or fragment that you want based on what they select.
This can't be done.
The DrawerLayout lives within your Activity and the Fragments that you switch to also live within the same Activity. This is the reason why switching fragments will leave the drawer intact. It's because they both exist within the same Activity without any interference.
However, launching an Activity is different. This is a completely different Activity which has it's own layout.
You actually only have two options if you wish to continue using a Drawer for main navigation.
Remove the need for the second Activity and change that to a Fragment. This way, all your fragments will exist within the same parent Activity so it'll use the same drawer that exists in that parent Activity.
Create an identical DrawerLayout and NavigationView in the second Activity. Call code to have the Drawer be opened when it's created. This way, although you're not really using the same Drawer, you're giving the illusion that it's still the same Drawer.

What are the advantages of using Navigation Drawer with Activities VS with Fragments?

I want to implement a Navigation Drawer in my app but I am conflicted on whether I should use it with Fragments or with Activities (see image below for more details).
Is there any real advantages or disadvantages between the two or is it just a matter of preference?
Edit:
Just to clarify my question:
In the case of using Activities instead of Fragments;
When I select "Import" that will open an Activity and not a Fragment and if I select "Gallery" it will open an Activity with contents for gallery item etc. and so on for the other items in the Drawer window.
In the case of using Fragments instead of Activities;
If I choose from any of the Items in the Drawer window it will open their contents in Fragments for each Item selected instead of starting new Activities for each selction.
Remember Fragments need an Activity. You always have one minimum when using Fragments.
If you are talking about to use like main element in the most cases is best use fragments because you have more flexibility UI.
The performance would be better if you have 3 activities and 10 Fragments or have 13 Activities? Think about it, the navigation within the App would be the big challenge but it's just about using the right flow in your application.
Edit:
For instance:
Drawer With Activities instead of Fragments
If you were to use NavigationDrawer without Fragments then it would be best to keep the NavigationDrawer instance in a single Activity and when you navigate the app by choosing from the items in the NavigationDrawer then each of those Activities that are started should not implement the NavigationDrawer but instead should implement the back button to Navigate back to the "Main"/single Activity that the NavigationDrawer was implemented in.
Note: If you do want to implement the NavigationDrawer in multiple Activities you would have to recreate a new instance of the NavigationDrawer in every Activity that you desire to display it.
I suppose this would be a disadvantage vs using Fragments whereas if you used a fragment you wouldn't need many instances of the drawer you would only need one.
Drawer With Fragments instead of Activities
If you use the NavigationDrawer with Fragments then the drawer should be implemented in a single Activity and when each drawer item is selected, their contents are displayed in each of their very own Fragments(which is called inside of the central Activity which manages all the Fragment instances)

Using fragments as activities in viewpager

I am currently having a mainactivity which holds another five activities in a listview. Upon clicking it will open the corresponding activities.
I actually want to use tabs with viewpager.
Now, Do I need to start my activities when clicking on each tabs
or I have to keep one activity and pass only the fragment to the
viewpager?

Can i pass an Instance of a FragmentActivity into a Fragment transaction

I’m trying to do a tab layout, but before I’ve a navigation drawer that contains some menu. In one of those items I want to display a Tab activity or let’s say view. My problem is in the replace method we should pass a Fragment and in a tabbed view you can't do it with a Fragment you should have an activity or a FragmentActiviy. Now I’m trying to found a solution to this.
There is nothing that actually forbids using a ViewPager inside a Fragment, but I'd try to avoid going down that road. I fear for the UX, having both a navigation drawer and tab navigation is very confusing.
If you must do it nonetheless, you can find an example of using ViewPager on the official documentation page here, it is using Activity but the only main difference I see when implementing that in a Fragment is that the FragmentManager you use should be the one obtained calling getChildFragmentManager inside the main Fragment.

Categories

Resources