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, MainActivity
and ShopActivity
. In MainActivity
I 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 MainActivity
each other Activity
.
So just code written MainActivity
in 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);
}
}