I found a lot of informations about the setRetainInstance method, but I'd like to know how to implement this method in a Navigation Drawer Activity?
I basically have:
An Activity, that contains...
a NavigationDrawerFragment, that contains...
a TopLevelMapFragment (showing Google Maps).
My aim is to prevent a reload of the whole maps and its tracks when the user rotates the screen. What is the basic concept of setRetainingInstance in an Navigation Drawer Activity? Do I have to call setRetainingInstance(true) in the NavigationDrawerFragment AND the TopLevelMapFragment?
I finally found the solution to my Problem:
When I created the Navigation Drawer Activity, I relied on the default Navigation Drawer Activity-template of Android Studio. In this template, the FragmentTransaction-replace method is called each time the user clicks on an item in the Navigation Drawer. Problem: the replace-method destroys all the fragments that are in the specified container.
So if you want to keep your fragments alive, don't ever use the replace method. You have to use the methods add, show and hide instead.
In addition, I call the setRetainInstance(true)-method in my Google Maps-Fragment, so that the fragment stays alive when the user rotates the screen for example.
Related
I have a problem...I am developing an app in android studio.In the meantime,there is already a navigation drawer in it.But the problem is, it only appears in the home screen.When it goes to another screen, the navigation drawer is not shown. I am new to android studio but i know some of Java languages.Can anyone help me with providing anything that would help me make the navigation drawer show in all of slides and pages in my app.
from docs
Navigation Drawer
The navigation drawer is a panel that displays the app’s main
navigation options on the left edge of the screen. It is hidden most
of the time, but is revealed when the user swipes a finger from the
left edge of the screen or, while at the top level of the app, the
user touches the app icon in the action bar.
for achieve what you asked you have to use Fragments
Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
see this examples for more details
1. Navigation Drawer - android hive
2. Navigation Drawer exp 2
3. Navigation Drawer exp 3
Refer this answer also
Answer
You have to add fragments in that activity where the navigation drawer exist. Whenver the user will click on the option in the navigation drawer the view should be changed by replacing with the required fragment.So by using the fragments user will stay on the same activity but just the views will be changed in that activity. you can refer to the fragments documentation provided by android developers. https://developer.android.com/guide/components/fragments.html
I know I can use a Fragment and have it added to the back stack, like so:
Fragment fragment = MyFragment.newInstance();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_holder, fragment).addToBackStack("my_fragment").commit();
Then I can handle changes in onBackStackChanged:
#Override
public void onBackStackChanged() {
boolean hasBackStack = getSupportFragmentManager().getBackStackEntryCount() > 0;
handleBackStackChanges(hasBackStack);
}
But the nagging question I have is whether or not I should.
The initial reason for this question is because of my desire to implement a persistant navigation drawer. One that is available at any point within the application, even if the navigation drawer icon (hamburger icon) is not present, with a swipe from the left edge of the screen.
This leaves me with two options; first I can have a single Activity that has a navigation drawer and hosts all of the Fragments, or I can have multiple Activities that each contain a navigation drawer.
Initially I was drawn to the first option, considering the navigation drawer sample uses fragments for each drawer option. This method would also allow me to easily implement the navigation drawer icon animation in onBackStackChanged:
public void handleBackStackChanges(boolean hasBackStack) {
ObjectAnimator.ofFloat(mDrawerArrow, "progress", hasBackStack ? 0 : 1).start();
...
}
But the sample is essentially a "flat" application, it doesn't handle any back stack changes (except for back to the MainActivity, which doesn't have a navigation drawer). And upon further development I have come across some difficulties with using a single Activity. Especially when handling orientation changes, and changes within the Toolbar.
So now I am thinking it may be simpler to use multiple Activities. Every Activity would extend a custom NavigationDrawerActivity. The MainAcitivity would have a Fragment for each navigation drawer item. Then each subsequent Activity (called from one of the Fragments in the MainActivity) would have a NavigationDrawer of its own, with a reference to which navigation drawer item the original Activity was in.
I can see this route presenting its own problems as well. Handling the navigation drawer icon animation, and handling the back stack when an item is selected from the drawer.
My question is essentially this: has anyone faced these problems already, and is there a more concrete solution? Are there issues (performance related or otherwise) that I could run into when choosing one option over the other?
I have my MainActivity that contains a navigation drawer with items. When I click on an item,it opens up a new activity. This activity has its own layout file. I want to call the navigation drawer in this activity as well. How do I do that? Because I want to navigate through the app using the navigation drawer,rather than pressing back button all the time.
Let's say I have 2 activities, both of which I want to have the Navigation Drawer. In the layout.xml for each, I specified a DrawerLayout with the appropriate list view to hold my navigation options. Essentially, the Navigation drawer is made every time I switch between activities, giving the appearance that it is persisting. To make life a lot easier, I took the common methods required to set up the navigation drawer and put them in their own class: NavigationDrawerSetup.java. That way my activities can use the same custom adapter, etc.
Complete Reference : NavigationDrawer with Multiple Acivities
I have made a nice app with a whole bunch of activities , then i needed a navigation drawer and found out that you need to have only one activity for the whole app and the individual different screens should be fragments that are inserted at runtime.
my question is :
How to convert the entire app to use fragments instead of activities ? (eg: how to preserve activity hierarchy , show a main activity when the user opens the app , different actionbar for each screen , etc...)
There is no magic converter, you need to convert manually each activity to be extended from Fragment
and add few must methods like onCreateView for the fragment instead of SetContentView of activity.
Regarding the actionbars, it sits on the Main Activity so you need to create callback events from each fragment to the main activity in order to control the action bar.
Navigation drawer has nothing related to fragments.
If you wish you can put it into activities also.
What I created was a BaseActivity with layout having navigation drawer and all other activities extend BaseActivity so that each of your activity will have drawer. Only you need to change content page for particular activity.
happy coding.
I have a Activity A, a ListFragment P and 2 Fragments Q and R.
When the app is launched, A is created, which loads P. Based on what user clicks, it is replaced by Q or R.
Now by referencing this tutorial, I have implemented a Navigation Drawer which shows certain items to the user. However, since I have implemented the Navigation Drawer in the Activity, it shows for all the Fragments.
I want it to be only available to P.
(Very much similar to Googles Gmail app. When the user is on the main screen - the drawer is present. When user taps to open an email, the drawer changes to back button)
I am not sure how to translate the above code. Any help is appreciated.
I solved this issue by simply overriding the up carat behavior by calling the mDrawerToggle.setDrawerIndicatorEnabled(enable) and passing the boolean enable or disable as needed.
(The fragments where I didn't want the drawer to show called this method with false and where I wanted the drawer to be shown called this method with true. I put the call inside the onResume() of the respective fragment for obvious reasons.)
This works exactly like I want, and I did not have to change the design of my project :).
What you can do is create a new FragmanetActivity S and replace Q and R accordingly. If you are app is for Android 3.0 lower user ActionBarActivity create a actionbar and set its setDisplayHomeAsUpEnabled(true). As the new FragmentActivity will have new layout there will be no NavigationDrawer.