0

I have 5 Fragments and of course my .MainActivity.

MainActivity
FragOne
FragTwo
---
FragFive

and then:

activity_main.xml
app_bar_main.xml
content_main.xml
nav_header_main.xml
lay1.xml
lay2.xml
---
lay5.xml

When the application is loaded it's a blank screen but then obviously when I click the navigation bar the pages will load.

My question is, is there anyway I can use a fragment as my launcher page, because if I tried making a home page on any other activity like content_main or acivity_main, obviously then it would show on every page which I don't want. But then if I made a new activity and set that as the launcher,obviously that would work but then my navigation bar would be missing I presume? What's the best way to go about this?

Petey987
  • 13
  • 1

1 Answers1

0

What you want is not that big a problem. Just load a fragment using SupportFragmentManager in your MainActivity. If this Fragment is a one time view only (before you change to your other fragments using Navigation Drawer), then a simple loading of your launcher Fragment in the MainActivity is enough.

Example for loading it once in MainActivity:

In your onCreate method in MainActivity :

///////////Setting up Fragment as Starting Page////////////
LauncherFragment fragment = new LauncherFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment,"launcher");
fragmentTransaction.commit();

This Launcher Fragment won't be seen after you change to other Fragments using NavigationDrawer (unless you call it somewhere else, which according to your question you don't want)


If instead of using separate LauncherFragment you decide you want to load FragOne itself, you will need to manually highlight your Drawer's First item (FragOne) after setting up FragOne

navigationView.getMenu().getItem(0).setChecked(true);       ////Set Navigation drawer manually
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
  • navigationView came up red, does something need to be imported? – Petey987 Feb 26 '17 at 17:28
  • You must have navigationView defined in your MainActivity, `NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this);` – Kaushik NP Feb 26 '17 at 17:30
  • navigationView is linked to your NavigationDrawer. – Kaushik NP Feb 26 '17 at 17:31
  • I do have it defined. – Petey987 Feb 26 '17 at 17:33
  • Cannot resolve symbol navigationView, am I putting navigationView.getMenu().getItem(0).setChecked(true); in the right place? – Petey987 Feb 26 '17 at 17:40
  • You sure its defined in your onCreate() in MainActivity? If it is, um, try to Rebuild your Project. – Kaushik NP Feb 26 '17 at 17:43
  • Do not use `navigationView.getMenu().getItem(0).setChecked(true);` until and unless you are loading FragOne instead of a new LauncherFragment! I specified that in the answer. – Kaushik NP Feb 26 '17 at 17:45
  • And add these codes after you see the declaration of navigationView in your onCreate() in MainActivity. If you put it before the declaration it'll put up an error. (think this is what might have happened) – Kaushik NP Feb 26 '17 at 17:47
  • Sure. Glad to be of help. Next time onwards, do try to give more clarity to your questions and ofcourse, **some Experimentation with the codes would have certainly got you this answer** :D – Kaushik NP Feb 26 '17 at 17:54