0

I start new activity in android studio auto generated MainActivity.java file, which has Navigation drawer.

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            startActivity(new Intent(getActivity(), Dishes.class));

This is my Dishes.java code

public class Dishes extends FragmentActivity {
//private Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //context = Dishes.this;

    //fragment main s shown, but no original navigation drawer??
    setContentView(R.layout.fragment_main);

    new getData().execute("..");
}

ActivityMain.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.app.MainActivity">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.app.NavigationDrawerFragment" />

</android.support.v4.widget.DrawerLayout>

Layout which is created in MainActivity.java (before Dishes activity is started

This layout is shown after i start Dishes activity. It has its own Actionbar w/o navigation

The problem is, that after i start new activity, the new layout is created and shown (i assume) and i cannot access old navigation drawer. How to fix it?

Rokas
  • 1,712
  • 2
  • 18
  • 35
  • Not even with the back button? – pedromss Feb 08 '14 at 11:15
  • Back button works (It gets back to first screen) – Rokas Feb 08 '14 at 11:16
  • In your onCreate() you don't call **setContentView(myLayoutFile);** – Phantômaxx Feb 08 '14 at 11:18
  • nothing changes if I `setContentView(R.layout.fragment_main);`. If I `setContentView(R.layout.activity_main);`, application breaks – Rokas Feb 08 '14 at 11:21
  • You must have a layout for your activity. Or it will show as blank. And I think that you can't reuse the layout from the calling activity. It seems that you tried to give the called activity the layout of the Fragment (??) and then of its container (??). This is clearly wrong. – Phantômaxx Feb 08 '14 at 11:23
  • What if I implement FragmentActivity instead of Activity? – Rokas Feb 08 '14 at 11:26

2 Answers2

0

As your MainActivity already has the drawer,
For being able to access the Navigation Drawer:
You need to extend MainActivity (make a base activity with the drawer and all classes would extend that)
In the layout xml, you can make one parent layout which has the drawer and a layout..eg LinearLayout which would be inflated dynamically depending on the layouts of the activities you call.
Refer:
How to add Navigation Drawer to all the activities in the application?
Open Android Navigation Drawer from an activity class
Android Navigation Drawer (calling activities) with AbstractMainActivity

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51
  • @Rokas please check the links in my edited answer, is your requirement different (?) – Pararth Feb 08 '14 at 11:33
  • It looks easy to implement, but it also looks dirty. Maybe I can achieve the same using `FragmentActivity` instead of `Activity`? – Rokas Feb 08 '14 at 11:39
  • Yes, instead of Activity, it'l be FragmentActivity and you will be creating Fragments, but i don't think there'l be much of a change in rest of the concept :) – Pararth Feb 08 '14 at 11:49
  • or..These might help you: http://manishkpr.webheavens.com/android-navigation-drawer-example-using-fragments/ http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ https://github.com/thecodepath/android_guides/wiki/Fragment-Navigation-Drawer – Pararth Feb 08 '14 at 11:53
0

Follow these steps:

  1. Make a FragmentActivity/ Activity depending upon the API you are targetting and in the drawer ( where I believe) you have a frame layout (if not then add) as the one which appears on top (i.e. when your app starts) and the drawer (which swipes from the left usually) which can be a list or another layout as per your requirement.
  2. In onCreate method of your FragmentActivity, load the tabs which is displayed in your 1st image above in this FrameLayout.
  3. Then depending upon the actions performed from the drawer or your tab activity, replace the the current fragment with the desired one in your FrameLayout.

This is the way I use Navigation Drawer and Fragments together. Let me know if its unclear or you need some snippets related to it.

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
  • Keep in mind that all the screens you would be using including the tabbed one should be fragments. :) – Atul O Holic Feb 08 '14 at 12:20
  • Ok, I made FragmentActivity instead of activity, added `setContentView` and now it shows the fragment that I want, but still no navigation drawer. I have updated my question. Can you add code snippet please? – Rokas Feb 08 '14 at 13:12
  • Great. I had the code but wanted you to try first. :) Please check this simple and yet awesome tutorial. Most importantly working. http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ – Atul O Holic Feb 08 '14 at 18:28
  • I am sure you will understand its working. Let me know if in case you have any difficulty. Once you are done I will help you to get the tabbed screen on your main page. Happ Coding. :) – Atul O Holic Feb 08 '14 at 18:30
  • Meh. It is soo simple. I cant believe that I never thought that I can create fragment in mainActivity. Learning mistakes :/ P.S. I have implemented my tabs, thank you for help :) – Rokas Feb 08 '14 at 18:44
  • 1
    Pleasure is mine. :) Hope you got the point of fragments too and the only reason I emphasized on FragmentActivity was to ensure that your app is backward compatible as well. Have fun. :) – Atul O Holic Feb 08 '14 at 18:57