When you start the application, an empty Activity is launched, how to start the first Fragment from the side menu?
Activity code:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { Fragment_Go fgo; Fragment_Programs fprograms; Fragment_Settings fsettings; Fragment_Statistic fstatistic; Fragment_Uprajnenia fuprajnenia; Fragment_zameri fzameri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); fgo = new Fragment_Go(); fprograms = new Fragment_Programs(); fsettings = new Fragment_Settings(); fstatistic = new Fragment_Statistic(); fuprajnenia = new Fragment_Uprajnenia(); fzameri = new Fragment_zameri(); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); FragmentTransaction ftrans = getFragmentManager().beginTransaction(); if (id == R.id.nav_go) { ftrans.replace(R.id.container, fgo); } else if (id == R.id.nav_programs) { ftrans.replace(R.id.container, fprograms); }else if (id == R.id.nav_uprajnenia) { ftrans.replace(R.id.container, fuprajnenia); }else if (id == R.id.nav_statistic) { ftrans.replace(R.id.container, fstatistic); } else if (id == R.id.nav_zameri) { ftrans.replace(R.id.container, fzameri); } else if (id == R.id.nav_settings) { ftrans.replace(R.id.container, fsettings); } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } ftrans.commit(); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } } Code Fragment_Go:
public class Fragment_Go extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, eg ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public Fragment_Go() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment Fragment_Go. */ // TODO: Rename and change types and number of parameters public static Fragment_Go newInstance(String param1, String param2) { Fragment_Go fragment = new Fragment_Go(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment__go, container, false); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } } Container markup:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.sharkitt.sportplus.MainActivity" tools:showIn="@layout/app_bar_main" android:id="@+id/container"> </FrameLayout>
onCreate()activate, register the launch of the desired fragment "directly", and not by pressing the menu button - pavlofff