The essence of the problem is the following: there is a main activity, and when you try to switch to another, all components of another activity simply overlap with the main one. How can this be fixed? I attach the code:

public class MainActivity extends AppCompatActivity { private TextView mDateDisplayStart; private TextView mDateDisplayEnd; private ImageButton mPickDateStart; private ImageButton mPickDateEnd; private String[] mScreenTitles; private DrawerLayout mDrawerLayout; private ListView mDrawerList; private android.support.v4.app.ActionBarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDateDisplayStart = (TextView) findViewById(R.id.dateStart); mDateDisplayEnd = (TextView) findViewById(R.id.dateEnd); mPickDateStart = (ImageButton) findViewById(R.id.showDateStartPicker); mPickDateEnd = (ImageButton) findViewById(R.id.showDateEndPicker); final Context context = this; mPickDateStart.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { MyDatePicker dlg = new MyDatePicker(context); dlg.setTextView(mDateDisplayStart); dlg.show(); } }); mPickDateEnd.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { MyDatePicker dlg = new MyDatePicker(context); dlg.setTextView(mDateDisplayEnd); dlg.show(); } }); mTitle = mDrawerTitle = getTitle(); mScreenTitles = getResources().getStringArray(R.array.screen_array); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); // Set the adapter for the list view mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mScreenTitles)); // Set the list's click listener mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); mDrawerToggle = new ActionBarDrawerToggle( this, /* host Activity */ mDrawerLayout, /* DrawerLayout object */ R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */ R.string.drawer_open, /* "open drawer" description */ R.string.drawer_close /* "close drawer" description */ ) { /** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { getSupportActionBar().setTitle(mTitle); supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { getSupportActionBar().setTitle(mDrawerTitle); supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; // Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle); // Initialize the first fragment when the application first loads. if (savedInstanceState == null) { selectItem(0); } } private void updateDisplay(TextView tv, Calendar c) { tv.setText(new StringBuilder() .append(c.get(Calendar.DAY_OF_MONTH)) .append("/") .append(c.get(Calendar.MONTH) + 1) .append("/") .append(c.get(Calendar.YEAR))); } private class MyDatePicker extends DatePickerDialog { public MyDatePicker(Context context) { super( context, null, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH)); } // Раскомментируй, если нужен такой конструктор // public MyDatePicker(Context context, @Nullable OnDateSetListener listener, int y, int m, int d) { // super(context, listener, y, m, d); // } private final Calendar c = Calendar.getInstance(); private TextView tv = null; @Override public void onDateChanged(DatePicker view, int year, int month, int dayOfMonth) { // По мере изменения даты, обновляем Calendar: c.clear(); c.set(year, month, dayOfMonth); // Если хочешь, чтобы дата менялась по мере ввода раскомментируй //if (tv!=null) updateDisplay(tv, c); } @Override public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE && tv != null) { updateDisplay(tv, c); } super.onClick(dialog, which); } public void setTextView(TextView textView) { tv = textView; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { // If the nav drawer is open, hide action items related to the content view boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); menu.findItem(R.id.action_search).setVisible(!drawerOpen); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Pass the event to ActionBarDrawerToggle, if it returns // true, then it has handled the app icon touch event if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action buttons switch(item.getItemId()) { case R.id.action_search: // Show toast about click. Toast.makeText(this, R.string.action_search, Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } } /* The click listener for ListView in the navigation drawer */ private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } } /** Swaps fragments in the main content view */ private void selectItem(int position) { // Update the main content by replacing fragments Fragment fragment = null; switch (position) { case 0: fragment = new ScreenOne(); break; case 1: fragment = new ScreenTwo(); break; case 2: fragment = new ScreenThree(); break; case 3: fragment = new ScreenFour(); default: break; } // Insert the fragment by replacing any existing fragment if (fragment != null) { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.content_frame, fragment).commit(); // Highlight the selected item, update the title, and close the drawer mDrawerList.setItemChecked(position, true); setTitle(mScreenTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } else { // Error Log.e(this.getClass().getName(), "Error. Fragment is not created"); } } @Override public void setTitle(CharSequence title) { mTitle = title; getSupportActionBar().setTitle(mTitle); } /** * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()... */ @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggles mDrawerToggle.onConfigurationChanged(newConfig); } public void Bronirovat(View view) { Intent nextActivity = new Intent(this, Bronirovat.class); startActivity(nextActivity); } } 
  • What role do you have in Navigation Drawer when changing activity? - KeyGenQt
  • As I understand it, you do not overlay it, but fragments? And the original fragment you have in the markup is specified? - Yuriy SPb
  • @KeyGenQT as a side menu - Kirill
  • @ YuriySPb Yes, the original fragment is in the markup - Kirill

1 answer 1

Most likely the fact is that the original fragment was added to the activation through markup. So do not do it. Add it programmatically by removing from the markup, something like this:

 Fragment initialFragment = getSupportFragmentManager().findFragmentById(R.id.container); if (initialFragment == null) { initialFragment = new FragmentInitial(); getSupportFragmentManager(). beginTransaction().add(R.id.container, initialFragment) .commit(); }