I have a simple application consisting of one MainActivity and two fragments, FragmentA and FragmentB. I want to use a NavigationGraph such that users still land on MainActivity but then can use two buttons to navigate to either FragmentA or FragmentB. Is there a way to do that? Based on the stuff on DAC, it looks like I have to use three fragments plus the MainActivity. But I was wondering if I can just use my existing scheme of 1 Activity and 2 Fragments.
Adding details to avoid confusion?
MainActivity has two buttons: User clicks on "Do A" to go to FragmentA and on "Do B" to go to FragmentB.
However, to use the Navigation Graph, it looks like I have to add one more fragment -- MainFragment. My question is: is there a way to avoid having to add an additional fragment and still use Navigation Graph.
Android applications need at least one activity to show UI. A fragment is just like a button in an android app. You cant show it without attaching it to an activity. In the android navigation tutorial, there's one activity. That activity only contains tag so it is actually empty. In fragment tag, you show a fragment and that tutorial show you how to switch fragment inside tag. So you are always inside MainActivity even when your app show a different UI(switching between fragment).
just like ianhanniballake comment above.. What does your activity show? What do you expect it to show? you always see it.
Edit:
Navigation graph only works for fragments. If you want to navigate between activities, use this Code:
Intent switchActivityIntent = new Intent(this, TargetActivity.class);
startActivity(switchActivityIntent);
You can use navigation graph like usual and when you need to open activity just use that code. It works fine.. But why do u need to use more than one activity when u using navigation graph? you should only have one activity when using navigation graph. That is why google make that thing(navigation graph) to support Single-Activity architecture.
Related
I'm using cicerone and single activity approach. When the application starts, the first three seconds I'm showing splash fragment inside my activity and so redirect to home fragment. Now I need to add to my application bottom navigation. Is it possible to add bottom navigation using single activity approach and cicerone. Or will I have to add another activity for the holding bottom navigation ?
Unfortunately, I could not find examples of projects with cicerone, single activity and bottom navigation.
My main goal is to save single activity, please tell me if it is possible to achieve this ?
General:
I have my app consisting of the activity and several fragments. I am using the Navigation Graph to move through the various fragments. I have the ability to rotate the device and therefore have different layouts.
Problem:
I know from fragment A I go to fragment B and I rotate the phone, I go back to fragment A
First partial solution:
From the documentation I took what could have been the solution, which however turned out to be only a partial solution. in fact I read that to prevent problems during rotation I could use the following parameters:
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
This would seem to solve the rotation problem but it creates a new one: landscape layouts are not updated to the size they should have.
How can I completely remedy the problem?
It doesn't matter if you need to get your hands dirty in the code or just add another parameter in the manifest, I would like to understand what is wrong
Example:
bottom navigation -> TabA (entrypoint);
TabB,
TabC.
I click on Tab B -> then clicking inside I will go to fragment B1 (from corrent tab B). So I rotate phone and I will go again to Tab B instead fragment B1
When the phone rotate, the activity is recreated, so I suppose that you activity is starting again from the entry point fragment. You can use ViewModel to persist the actual fragment reference and inflate it again on OnResume method of the Activity who hold the fragments.
i have to jump from activity A to fragment2 in activity B(in the activityB, i use the viewpager,which contains four fragment).what i want is when i use the upNavigation of the actionbar ,i just jump to the exact second fragment.
can anyone help me?(i have searched for some related questions ,but it seems they all navigate in/between the fragments,which is not my want.)
Use this , detect the onclick
ViewPager.setCurrentItem(pageNumber);
above line will navigate you to that page
I am trying to implement a navigation drawer (left slide menu) in my Android project. My application has a main activity till now, in which you could navigate to others with Intent method through buttons.
So, I searched for a navigation drawer and I found/followed the exact steps of THIS tutorial. My code related to the menu is exactly the same, so for that reason, I am not posting it below.
Related to my code:
The three fragments that I created, were just extended the class Fragment and where completely empty.
The ONLY change from the code in the tutorial, is inside the res/layout/activity_main.xml, because I changed the com.codepath.examples.navdrawerdemo.FragmentNavigationDrawer to 'my_project's_name'.FragmentNavigationDrawer.
What I managed to do:
I managed to create the menu that I want and display it ONLY if I make the MainActivity.java as the launcher inside the AndroidManifest. When I see the menu, I can go to each fragment through it, but I can't go to any of my Activities because I didn't link the fragments with the activities that I want. My menu, also does this transition that it should, so I am completely fine with the style of it.
What I want to do:
I just want to have the same menu inside every activity that I have already created, and when I click in an item in the menu, I want to navigate to an activity that I want BUT I want the activity to still display the menu.
I searched for solutions, but when I put back my first Activity as launcher in the Android Manifest, and set a layout for example inside the Fragment for it, it's just a useless layout. And when I tried to do an Intent to call my Activity inside the Fragment, the menu disappeared.
P.S I didn't changed any of my Activitie's code or of their layouts. I added only the code from the tutorial!
I'm currently porting iOS App to Android
the iOS app uses tabbar + navigation controller (inside each tab) to load multiples Views
I would like to keep the same design on Android, but I'm a bit confused with it
I tried "startActivityForResult" to load another activity the TabHost is removed
I also tried to replace the view with setContentView, I'm working, but as some tabs requires more than 10 sub screen, the code will be very elegant.
So I'm looking for a solution to load another Activity in the same tab with a UINavigationController like features
Ex: If I have three tabs A, B, C, on tab A I can go to A1 while pressing some button, If I push back button, I would like A to be displayed
You can go either with Fragment or with ActivityGroup.
Fragment is the latest solution where as ActivityGroup is deprecated.
You can refer:
Activity Group => this, this, this, this
Fragment => this, this