Sidebar menu disappears when switching activities


Mohamad Mousheimish:

I know this question is asked a lot, but none of the answers worked for me.

I have two activities, MainActivityand ShopActivity. In MainActivityI have a sidebar menu.

When I switch activities, the menu disappears and the problem happens.

So how can I pin the sidebar menu in each activity? Is this impossible?

Main activity onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDrawerLayout = findViewById(R.id.drawer);
    mNavigationView = findViewById(R.id.navigationView);
    mNavigationView.setNavigationItemSelectedListener(this);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    CreateExampleList();
    BuildRecyclerView();
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
    }
}

This is where I want to switch the activity:

public void BuildRecyclerView() {
    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    adapter = new ShopAdapter(shops);
    adapter.setOnShopClickListener(new ShopAdapter.OnShopClickListener() {
        @Override
        public void onItemClick(int position) {
            ShopItem selectedShop = shops.get(position);
            Intent k = new Intent(MainActivity.this, ShopActivity.class);
            startActivity(k);
            Toast.makeText(MainActivity.this, "Selected Shop: " + selectedShop.getShopName(), Toast.LENGTH_SHORT).show();
        }
    });
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
}

Second activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop);
    mDrawer = findViewById(R.id.drawer);
    mToggle = new ActionBarDrawerToggle(this, mDrawer, R.string.open, R.string.close);
    mDrawer.addDrawerListener(mToggle);
    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.activity_shop, null, false);
    mDrawer.addView(contentView, 0);
}

Does anyone know what's wrong?

Mohamad Mousheimish:

The solution is to simply initialize the menu bars the same way as MainActivityeach other Activity.

So just code written MainActivityin Second Activity .

The second activity onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDrawerLayout = findViewById(R.id.drawer);
    mNavigationView = findViewById(R.id.navigationView);
    mNavigationView.setNavigationItemSelectedListener(this);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    CreateExampleList();
    BuildRecyclerView();
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
    }
}

Related


Sidebar menu disappears when switching activities

Mohamad Mousheimish: I know this question is asked a lot, but none of the answers worked for me. I have two activities, MainActivityand ShopActivity. In MainActivityI have a sidebar menu. When I switch activities, the menu disappears and the problem happens. S

UISearchBar disappears when switching views with UISegmentControl

Brittany I have a UISearchBar (& UISearchDisplayController) inside the tableView sidebar I created (see photo). When the button in my UINavigationBar is clicked, it is visible. Inside my map view controller, I have a UISegmentControl in the navigation bar that

Crash when switching activities

and When I run the app using Android Studio, it crashes when switching from PeriodicTableScreen via onClick created in createButtons.java. Performance: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/andro

Can't swipe when switching to other activities

Preti My requirements:- I have two activities. When I swipe it goes to another activity. So, I have: -MainActivity.java public class MainActivity extends Activity { private GestureDetectorCompat gestureDetectorCompat; @Override protected void onCreate(Bun

Code disappears when switching queries on SQL Server

alexis lin I'm having an annoying problem with SQL Server 2016. It's not bad, just annoying. So when I switch the query window and even reduce the SQL server's window, I'm back to this one: code can disappear Random color appears on code and hides it I have to

Sidebar disappears when scrolling to top

Hiroto Yamada I'm working on the site now, where I followed the supplemental instructions until I found the footer. Demo Site ID: Passed Test: 2015 I'm doing it fine, but when I scroll back to the top (very top), my sidebar automatically hides it. When I scrol

R shiny: slickROutput disappears when switching tabpanel()

Danielle I'm making an application that embeds in . In each , I generate a sequence of images. When switching from one panel to another, images loaded in one of the panels disappear.ShinytabPanelsnavbarPagetabPanel I have to manually "refresh" the page to see

Crash when switching activities

and When I run the app using Android Studio, it crashes when switching from PeriodicTableScreen via onClick created in createButtons.java. Performance: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/andro

Getting NullPointer exception when switching between activities

username I have 3 activities Activity 1 Active 2 Activity 3 Here's the code: Activity 1: Intent intent= new Intent(Activity1.this,Acivity2.class); Bundle b=new Bundle(); b.putParcelableArrayList("actionArray", (ArrayList<? extends Parcelable>) AllLi

Getting NullPointer exception when switching between activities

username I have 3 activities Activity 1 Active 2 Activity 3 Here's the code: Activity 1: Intent intent= new Intent(Activity1.this,Acivity2.class); Bundle b=new Bundle(); b.putParcelableArrayList("actionArray", (ArrayList<? extends Parcelable>) AllLi

Fragment activity disappears after switching activities

Tony My navigation drawer has 6 fragments, one of which contains 2 other fragments, which are available to the viewPager. My problem is that when I switch from the fragment containing the viewPager (HomeFragment) to another and back, the contents of both fragm

Crash when switching activities

and When I run the app using Android Studio, it crashes when switching from PeriodicTableScreen via onClick created in createButtons.java. Performance: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/andro

Android App crashes when switching between activities

Saad Rehman When my app suddenly started crashing after the first page it worked absolutely fine. The first page only contains a button that takes it to the next activity. Helpppp package com.example.assignmenttwo; import android.os.Bundle; impor

App crashes when switching activities

Cristiano Wilson Well, my app crashes when I click the link to the new button Activity. Here is all my code: MyActivity: public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sa

App crashes when switching activities

Cristiano Wilson Well, my app crashes when I click the link to the new button Activity. Here is all my code: MyActivity: public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sa

Can't swipe when switching to other activities

Preti My requirements:- I have two activities. When I swipe it goes to another activity. So, I have: -MainActivity.java public class MainActivity extends Activity { private GestureDetectorCompat gestureDetectorCompat; @Override protected void onCreate(Bun

Navigation bar appears briefly when switching activities

Hunter S I'm currently writing an Android app for a Nexus 7 tablet that has a full-screen experience (the system and navigation bars are not displayed until the user swipes down). For some reason, when I switch activities, the navigation bar appears briefly. T

Sidebar menu disappears when switching activities

Mohamad Mousheimish: I know this question is asked a lot, but none of the answers worked for me. I have two activities, MainActivityand ShopActivity. In MainActivityI have a sidebar menu. When I switch activities, the menu disappears and the problem happens. S

Sidebar disappears when scrolling to top

Hiroto Yamada I'm working on the site now, where I followed the supplemental instructions until I found the footer. Demo Site ID: Passed Test: 2015 I'm doing it fine, but when I scroll back to the top (very top), my sidebar automatically hides it. When I scrol

Crash when switching activities

and When I run the app using Android Studio, it crashes when switching from PeriodicTableScreen via onClick created in createButtons.java. Performance: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/andro

Crash when switching activities

and When I run the app using Android Studio, it crashes when switching from PeriodicTableScreen via onClick created in createButtons.java. Performance: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/andro

Fragment activity disappears after switching activities

Tony My navigation drawer has 6 fragments, one of which contains 2 other fragments, which are available to the viewPager. My problem is that when I switch from the fragment containing the viewPager (HomeFragment) to another and back, the contents of both fragm

Getting NullPointer exception when switching between activities

username I have 3 activities Activity 1 Active 2 Activity 3 Here's the code: Activity 1: Intent intent= new Intent(Activity1.this,Acivity2.class); Bundle b=new Bundle(); b.putParcelableArrayList("actionArray", (ArrayList<? extends Parcelable>) AllLi

Navigation bar appears briefly when switching activities

Hunter S I'm currently writing an Android app for a Nexus 7 tablet that has a full-screen experience (the system and navigation bars are not displayed until the user swipes down). For some reason, when I switch activities, the navigation bar appears briefly. T

Out of memory when switching activities

Thomas I am making an android app that has a menu activity and a game activity. Switching from the menu to the game and back to the menu works, but when you try to go back to the game activity you get an out of memory error. The game activity loads some bitmap