What I am trying to do, is have a NavigationDrawer menu like this:
I've figured out how to change layouts when menu items are clicked, but how do I load a new activity without losing the menu?
My main question:
For example let's say one of my menu options takes you to a layout where there are some buttons that do something. I need to load an accompanying activity/class, that will handle the actions and functionality of that specific "page" in the App. If another menu option takes you to a layout with only an image, for example, that would be another activity without all that code that handles the functionality of the buttons, since there are no buttons in this screen.
I hope that makes sense! I followed many tutorials/videos online using several methods (Fragments etc.) but nothing quite answered my question.
From what I understand, you want the navigation drawer to be present in every activity. There are two methods:
Use @Russell's answer. Make a main activity having a framelayout usually called content_frame which covers the whole activity. This activity has the code for the navigation drawer. On button click you can replace the contents of this layout with the layout of the desired fragment (i.e. either fragment with several buttons or say an image). So the elements in the drawer are all fragments. In the tutorials the fragments are called through getFragmentManager(). Check out the video series on navigation drawer by this guys, slidenerd: https://www.youtube.com/watch?v=K8hSIP2ha-g . Try to implement it as you are going along the video
I personally prefer this method but it has its limitations. Create a baseactivity in which the code for the navigation drawer is. This has a framelayout usually called content_frame which covers the whole activity. The activities that need to have the drawer extend this baseactivity instead of appcompatactivity or activity. The layout inflation works like this in the oncreate:
getLayoutInflater().inflate(R.layout.activity_this, contentFrameLayout); instead of
setContentView. Here the activities are started through
startActivity. Cons of this method: a) the BaseActivity is destroyed and recreated every time the user changes activity. b) The activity can extend only one class which by default become baseActivity Pros: a) You can maintain an activity stack b) Each activity can have it's own configuration change rules and onsaveInstance rules. c) These activities can have separate fragments, which use this activity for communication. Trying to do this in the first method would involve main activity implementing huge number of interfaces unnecessarily (you will learn about interfaces in inter-fragment communication)