In order to have a persistent toolbar in my app (I want to animate/morph the icons on the toolbar when switching to a different screen), I used an architecture which consists of 1 activity, 1 toolbar and a main fragment, which gets replaced as needed.
In order to create proper navigation (including the device's back button) I need to let the activity (and/or the toolbar) know which fragment is currently being displayed.
I was thinking of creating a private Integer ArrayList - could be called fragmentNumberHistory - which stores the history of the fragment order displayed with the last being the current the number of the current fragment, simulating the back stack a little bit, only for "fragment id's".
Question: although this strategy seems to work, it feels like an unclean work-around. Is there a textbook way to implement this? I would have thought one could ask the FragmentManager which fragment is currently being displayed in a specific container but that doesn't seem to be the case?
I would recommend you to implement an interface to manage backstack.
Here is a good blog post which would help you understand this process
Related
I am currently writing an Android app for my Masters final project. The app has two Activities and both have layouts corresponding to each activity. I also have a settings activity with settings fragment but I am not concerned about this one.
Activity A currently has a Spinner and a button which when pressed will do some stuff and then launch a Activity B. Activity B displays a chart, contains a couple of actions and a button to go back to Activity A.
Neither of these activities have Fragments currently but I am curious if it would be better to include Fragments. As far as I can tell using a Fragment wouldn't hinder performance so at this point it would be a cosmetic change.
Fragments are usually needed if you need to re-use some specific layout.
E.g--> If you have an app which displays movies, you can click and get movie details.
Here it would be better to use fragments as the layout would be the same for each movie and only the content would change inside.
In your case however, since there is not need for such frequent scenarios, you really do not need fragments.
If you want to change the content without changing your activity due to some actions of user, implementing UIs inside Fragments can be useful. Just set the the desired fragment inside Activity by using its FragmentManager. However, otherwise don't think about such change (shifting lots of code/layout from activities to fragments) in the code.
I have one doubt and want to clear some points regarding the Activity holding multiple fragments.
I have 10 fragments attached with one Activity (HomeActivity.java); this activity contains one navigation drawer and tool bar with multiple icons like search, add, remove, back button etc.
The structure which I am following is below,
User clicks any item from navigation drawer, I am loading the fragments,
fragmentManager.beginTransaction().replace(R.id.fragment_container, fragmentToLoad, tagFragment).addToBackStack(tagFragment).commit();
fragmentToLoad -> fragment name, tagFragment -> tag to that particular fragment.
I am changing the title name, toolbar based according to the fragments in HomeActivity itself.
My navigation drawer items are having one fragment only and get replaced in fragment_container but one thing is also there, that these fragment are also having inner sub-fragments too.
In the start, I said that there are 10 fragments, those 10 fragments are navigation drawer items. There are many inner framgents.
Now, the inner fragment's click listeners and other activities I am maintaning in HomeActivity.java itself using interfaces(Listeners).
SO, some times I am getting warning "There is a lots of work going on main thread".
I understand that there is lots of activities going on HomeActivity.java.
Please tell me this architecture is wrong or is there any solution.
I wouldn't say that your architecture is WRONG. Instead, I would say that by using single central activity to handle so many fragments, you are making maintaining and debugging that activity very difficult for yourself.
As far as the warning message is concerned, it depends upon the number of frames skipped (something that is printed before 'There is a lots of work going on main thread' in the log). I believe if the number is ~ 100 then it really shouldn't be a matter of grave concern. Kindly go through this SO answer for complete explanation! Hope that helps.
I know it was answered before but I am still confused, if I should use fragments or activities for navigation drawer.
What is better practice? Google does not say anything about it and I am a little dissapointed.
Also, if I create a new project from Android studio with navigation drawer template, what I get is one single activity and one single fragment, but there is also separate fragment for navigation drawer and inside it, there is an interface and above it is a comment saying: "Callbacks interface that all activities using this fragment must implement.". It confuses me even more because I think I should use activities, but I am not sure.
I will keep it very simple: When you switch between activities , user has a bit of feeling as if we are taking him to another view(as if we are making him switch somewhere and the whole view is changed with a sudden blink) but when you do same stuff in one activity and changing views through fragment, it is very smooth. Moreover passing data from one fragment to other is very easy and less expensive as android says activity transactions are expensive.
Yet many times it depends on your requirement.
Keep in mind that a nav-drawer can be used for different user interactions. By the common usage as a navigation element, you will implement it by fragment(s). In this approach the drawer is placed on the left side.
An other approach can be to perform actions by pressing an drawer entry. In this situation you wont replace any fragments and only implement the drawer to the activities which should be able to perform this particular interaction (maybe: "send contact per a email")
I'm trying to work out the best way to implement the 'ancestral' and 'temporal' navigation (utilising the up button and back buttons), for a music player I'm working on.
Currently, the user is presented with a viewpager, and can page between three main fragments (ArtistMain, AlbumMain and SongMain). Upon choosing an item inside that view, a fragment transaction occurs, and the viewpager goes out of view, replaced by a new fragment (AlbumSub, Songsub or player, depending on where the user came from). The user can then navigate deeper, until a song is chosen, and then they are taken to the 'player' screen.
I guess the question is: How do I implement all of this conditional navigation?
I'm fairly new to android and programming in general, and I just can't seem to come up with an efficient way to achieve this. At the moment, as each fragment is brought into view, the app is checking to see where the user just came from, and then determines where the user should be taken if back or home is called. This means I have a booleans like "fromArtistMain", "fromAlbumSub", and I'm checking for things like "fromSongSub && fromPlayer".. it's all turning into a bit of a mess.
I've drawn a diagram (in paint, sorry!!), to depict the navigation I'm trying to achieve. The yellow represents the 'up' button press, the red is the 'back' button press, and blue is just normal navigation. The green arrows are meant to represent the view paging:
Any advice is welcome. It might take something really simple that I've just overlooked.
Thanks for your time.
Edit:
I have been adding fragments to the backstack, and using popBackStack() calls, the problem is that popping the backstack is not necessarily the correct option in each case.
I've currently got a whole mess of code trying to determine whether a transaction should be added to the backstack in the first place.
Consider the following:
User chooses a song straight from 'SongMain', and is taken to 'Player'. Now using the home button should (in my mind), take the user to SongSub (the list of songs from the album that the chosen song belongs to). Now if the user navigates up again, they will be taken to 'AlbumSub', the list of Albums by that artist. This is a fragment transaction, but adding to the backstack would mean the user would be taken down a level on back press (which I think would be unexpected). So in this case I don't add that particular transaction to the backstack - seems fine, but there are quite a few different cases, and combined with a viewpager at the top which needs to come in and out of visibility, the code gets really messy.
All of this means a whole bunch of conditionals determining where the user came from and which path they took to get there..
Would it be wise to have a bunch of booleans in the host activity which get set depending on where the user has navigated, and then checking those booleans to see if a transaction should be added tot he backstack? This is kind of what I already have, but it seems really inefficient.
What you're looking into is called the Back stack. You can read more about it here at developer.android.com.
If you use a single Activity to host each of these Fragments then you can modify your code to explicity add your Fragments to the Fragment Back stack using code like so:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack("NameOfFragment");
transaction.commit();
So, if you do the above code with Fragments like so:
Fragment1 -> Fragment2 -> Fragment3 -> Fragment4
Then from Fragment4 if you call this method:
getSupportFragmentManager().popBackStackImmediate();
Then Fragment4 will be finished and Fragment3 will restart. Simple. You can have this line called from a button click or you can override the behaviour of the back button to call it.
Please note in the examples I've used the function getSupportFragmentManager() which is a method name in the Compatibility Package. If you're not using the Compatibility Package then you can instead call getFragmentManager().
EDIT
The problem with the navigation you envisage is that breaking out of the backstack paradigm half way through means that your app will "Act Differently" than the rest of the OS. This is by and large discouraged by Google. But then again, saying that, I do exactly the same in my app for very similar reasons :).
When you navigate "up", along one of your yellow lines, you are following a discrete link (so, startActivity(new Intent(this, SongSub)); or whatever) and you want this to "break" the backstack.
It's at this stage you can make a decision about how you want to go forward:
You can start a Task (backstack) using SongSub as 0th item. This is from memory what the Google Music app does and you're right, it's annoying. When you press back it should technically exit the app. Yuk. IMO if you're in an obvious page hierarchy, back should always navigate down the hierarchy over exiting the app.
You can start a new Task using ArtistMain as the 0th element and layer fragments discretely ontop before commiting your transaction, in effect creating a new backstack each time you go "up" rather than "back" (your backstack would now be ArtistMain->ArtistSub->SongSub). This is what I think your trying to ask here. It's possible but it's messy.
You can create a more linear structure (probably the best idea if possible). Ignore the backstack paradigm, make "back" and "up" always go up a level no matter where you came from (Player always goes to SongSub, SongSub always goes to AlbumSub). This will give the user the least confusing and most transparent (as well as easiest to implement) experience - the user will learn quickly how to navigate (and how many "backs" to press) to get to where they want to be.
I have a special flow of fragments in my app, so I need to be able to switch to any fragment, while keeping fragments in memory whenever possible (so if it's tight on memory, it's ok that the fragment will be released).
So far, I've succeeded doing a replace of the current fragment, but the thing is that the previous fragment is always being destroyed, so if I go back to it (using the action bar, for example), it's re-created and that takes some time.
The reason I use fragments instead of activities is the nice usage of the action bar, the ability to put multiple fragments inside the same container, the non-flexible activities-intents usage, etc...
The reason why I don't use the "Back" stack is that I wish to go to any fragment from any fragment, since the flow can change.
Here's a snippet of my code:
Fragment fragment=... ; //get the fragment from cache or create if not available yet...
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainActivity_fragmentContainer, fragment).commit();
I have also tried to use ViewPager, but since I don't want to have the sliding effect (which allows you to slide to other fragments) and since one of the fragments already include a viewPager, it's an obstacle. Disabling the sliding effect on the main viewPager somehow disables it on the other one.
BTW, I'm using the android support library for the fragments and not the native API.
my question is:
how can i achieve full control of which fragment to go to , while maximizing speed and avoiding too much memory being used for fragments ?
EDIT:
for now , i use the next workaround :
for the onDestroyView , i take the parent of the created view and remove the created view from there .
for the onCreateView , if i already have the created view from before , i return it .
however , i think it's a very risky thing to do , since i'm not sure of how fragments managing work , so it might cause weird problems . plus ,i'm not sure what will happen if android decides that it has low memory - will it destroy unused fragments (which is good) or will it cause out-of-memory exceptions (which is bad) .
I agree with your comment regarding "it's very risky". Since the consequences of your approach are not really clear, I would not recommend using this.
To better understand your issue and to give you some indications:
what do you consider to be slow? I keep switching fragments "like crazy" too, and it works OK on a tablet, however on some low-end phones it takes much longer - but is still acceptable
How much work do your fragments need to be created? ie what kind of underlying data needs to be preapared each time? (again, I have several list fragments with more than 1k entries, they get prepared each time, and it's quite fast)
how do you exaclty replace fragments? In particular do you call getFragmentManager().executePendingTransactions();
One way to accomplish your goal is to use FragmentTransaction.show and FragmentTransaction.hide. You can use Fragment.isAdded to determine whether to show or to call FragmentTranscation.add the first time. The downside is that you won't get Pause/Resume events when fragments are shown and hidden; you will need to override onHiddenChanged. You will also have to manage your own stack if you want to support back navigation.