• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Using Intent to load a Navigation Drawer from the Main Activity?

How do I call a Navigation Drawer Activity from the Main Activity? When loaded on a Android device, the button doesn't work.

Code:
public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void GO()
    {
        Button GoButton=findViewById(R.id.buttonGo);
        GoButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(MainActivity.this, NavigationActivity.class);
                startActivity(intent);
            }
        });


    }
}


I apologize for my vagueness. In all the tutorials I've looked at, they had the main activity be the navigation drawer, which makes sense for a tutorial. What I want to do is different. From the main activity I want to start another activity from which you utilize the drawer rather than have access to it from the main activity. Main Activity--loads Main2Activity--Access Navigation drawer.
I thought all I had to do was to use Intent to start the activity, but the button does take me to the NavigationActivity, it doesn't go anywhere. That is the crux of my problem. Can anyone help. Thanks!
 
Last edited:
There's not really enough code to answer this. How is method GO() being called?
When you say it doesn't work, what happens?
 
If Navigation Drawer is already presented in your design you don't need to start Navigation Activity. Just open Navigation Drawer with openDrawer() method.
If want to start new Activity with Navigation Drawer and show this drawer, place openDrawer() method into onCreate() method of Navigation Activity.
Please find demo here https://github.com/v777779/aad_20180429
master branch Navigation Drawer presented in Activity and it opened by go() method.
android_wo_drawer branch Navigation Drawer is absent in MainActivity, but presented in NavigationActivity. Navigation Avtivity started by go() method. After that Navigation Drawer is opened inside Navigation Activity in onCreate() method.
Project for Android Studio 3.1. Checkout master branch or android_wo_branch to open relevant version of project.
 
There's not really enough code to answer this. How is method GO() being called?
When you say it doesn't work, what happens?
The go button is on the mainactivity xml sheet. The button is defined and calls an OnClickListener, which opens an Intent to launch the Navigation activity. However when you click the button, it does nothing. Does that make more sense?
 
If Navigation Drawer is already presented in your design you don't need to start Navigation Activity. Just open Navigation Drawer with openDrawer() method.
If want to start new Activity with Navigation Drawer and show this drawer, place openDrawer() method into onCreate() method of Navigation Activity.
Please find demo here https://github.com/v777779/aad_20180429
master branch Navigation Drawer presented in Activity and it opened by go() method.
android_wo_drawer branch Navigation Drawer is absent in MainActivity, but presented in NavigationActivity. Navigation Avtivity started by go() method. After that Navigation Drawer is opened inside Navigation Activity in onCreate() method.
Project for Android Studio 3.1. Checkout master branch or android_wo_branch to open relevant version of project.
I wanted to launch the NavigationActivity to utilize the navigation drawer from there, not from the main activity. Is that even possible?
 
The difference in main_activity.xml file and corresponding code of MainActivity.

1. master branch. You use Navigation Drawer of MainActivity. So you just open/close view.
Navigation Drawer placed in activity_main.xml of MainActivity.
Direct link: https://github.com/v777779/aad_20180429/tree/master
2. android_wo_drawer branch. You call NavigationActivity from MainAvctivity.
NavigationActivity opens it's own NavigationDrawer at start.
Navigation Drawer placed in activity_navigaion.xml of NavigationActivity.
The difference, when you close drawer, you are in NavigationActivity, not MainActivity.
Direct link: https://github.com/v777779/aad_20180429/tree/activity_wo_drawer
 
Last edited:
The difference in main_activity.xml file and corresponding code of MainActivity.

1. master branch. You use Navigation Drawer of MainActivity. So you just open/close view.
Navigation Drawer placed in activity_main.xml of MainActivity.
Direct link: https://github.com/v777779/aad_20180429/tree/master
2. android_wo_drawer branch. You call NavigationActivity from MainAvctivity.
NavigationActivity opens it's own NavigationDrawer at start.
Navigation Drawer placed in activity_navigaion.xml of NavigationActivity.
The difference, when you close drawer, you are in NavigationActivity, not MainActivity.
Direct link: https://github.com/v777779/aad_20180429/tree/activity_wo_drawer
Just wanted to follow up with you v. I integrated it into my app and it WORKS! Again thank so much!
 
Back
Top Bottom