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
Related
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.
In my app I implement a set of tabs to show activities instead of switching between fragments. All of these activities have a back button that I want to take the user out of the tabbed set of activities to where they were before they got to those screens. Is there a way I can easily finish all of the activities representing the tabs?
I looked into tasks but it seems like you can't just destroy a task and instead you have to start an activity in that task using CLEAR_TOP or CLEAR_TASK or something similar.
EDIT:
We use activities instead of fragments because each tab will be very large and complex/fully featured sections of the app that we thought would be better suited to an activity. The activities are started with REORDER_TO_FRONT. We do not want to use CLEAR_TOP since we would have to do a lot of requerying of data everytime we switched tabs. So for an example of the problem flow:
came from activity X and started activity A (the landing activity for the set of tabbed activities)
click a tab and switch to activity B stack looks like X->A->B
click third tab stack looks like X->A->B->C
when we click a "back" button on screen, all three activities should be removed from the stack so it becomes just X. But if we call finish it will make the stack look like X->A->B which we do not want.
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 have 3 fragments : A, B and C.
A contents a list of element, when you chose an element from the list, it loads B fragment. Inside B, i have a button showPreview, a click on that button loads the C fragment.
I already implemented all of this, and it's working pretty nice.
Now what i want is to add, in A fragment, a shortcut button on each item, to access the preview (C fragment) without showing B fragment on UI like in google play application, you can download an app by clicking on Three dots -> Install, without opening app detail page.
Presently, when user clicks on shortcut button, i load the B fragment first (shown in UI), after i call previewButton.performClick() to click programmatically on the showPreview button. But that's not what i want because i am obliged to show B first, let it load entirely before making a performClick().
I have read about FragmentTransaction methods (attach/detach, add/remove etc.), about fragment lifecycle, etc ... without solution.
So my questions are :
how can i load B fragment without showing it on the UI ?
If that's impossible, how to do the same thing as google play application ?
Finally I resolved the issue. My problem was that I haven't seperate Views from Model. I didn't have to use performClick() anyway, I didn't have to depend on Button click.
I resolved it by just creating a method which does nicely what I want, after I use this method on button click and after shortcut list choice.
I am creating an app for Android 3.0+ that uses the action bar with tabs for navigation. I have 2 tabs that each load a ListFragment:
Tab 1 Tab2
A B
C D
Where A & C are list items in Tab 1 and B & D are list items in Tab 2.
Currently if you select Tab2 and then item B a new ListFragment is loaded on top of the current list fragment in Tab2. Pressing back takes you back to the original Tab2 contents. However if you don't press back and instead select Tab1 you see Tab1's contents on top of the new content you loaded in Tab 2. So I am guessing there is a better way of doing this. I wanted to use multiple fragments in each Tab to reduce the code complexity in each tab (for instance the onListItemSelected handling). Is there another way to do that without using multiple fragments? I am also trying to just save state but I am not sure how to tell the difference from when the user clicks back (then I would want to restore state) and the user has selected tab 1 and then selects tab 2 again (i would want to restore state).
Thanks
I am having similar struggles with the complexity of fragments and tabs on the Action bar. It seems to me that android for tablets is still very much in its infancy and there are various issues (like yours and the issues I'm having) that will be addressed in the coming months. For now, I think you just have to handle some of those complexities yourself instead of relying on the Android framework.
To answer your first question more directly, I think you need to step back and think out the functionality before diving into the code. I can give you a potential solution (keep track of your fragments manually in the activity and simply add and remove fragment as necessary via the ActionBar.TabListener interface.
Also, if you want the user to be switching from one fragment to another like you are suggesting, perhaps the back functionality shouldn't be used. What happens if the user clicks on the tabs 10 times in a row. Do you want the back button to take the user back through all of those events? I think the back button should only be used when the navigation system is very sequential and "back" is an easy concept. Check out all the Google apps that use tabs to see what I'm talking about.