I implemented Navigation Drawer in my app. It's just a sample app, auto-generated Navigation Drawer fragments and activities from Android studio. I'm starting an activity from a section list item like this:
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.pocetna);
break;
case 2:
mTitle = getString(R.string.oglasna_ploca);
break;
case 3:
mTitle = getString(R.string.e_novine);
break;
case 4:
mTitle = getString(R.string.portal);
break;
case 5:
mTitle = getString(R.string.raspored);
startActivity(new Intent(this, RasporedWebView.class));
break;
}
}
When I use the back button, could I get back to let's say case 1 or even MainActivity (closing navigation drawer), because when I call the activity, and go back it returns to a blank activitity (or w/e), and then I must click back button once more. I tried searching for solutions, but couldn't find any.
Thanks in advance.
Refer this Docs.
Then add the below code to do Back button in Activity
ActionBar actionBar;
actionBar=getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Related
I have a bottom navigation bar with 4 fragments and 1 activity inside another activity(in which all fragment will be displayed). I want my first fragment to be displayed on the starting of the activity(in which all fragment will be displayed) along with the matching item of bottom navigation bar. My fragment 1 is getting displayed on starting but with wrong item of the bottom navigation bar.
This is what, I am getting on starting. Selected item should be Home(middle)
I have this under OnCreate
btmNav = findViewById(R.id.btmnav);
btmNav.setOnNavigationItemSelectedListener((navListner));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();
and this outside Oncreate
private BottomNavigationView.OnNavigationItemSelectedListener navListner = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navprofile:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment2()).commit();
break;
case R.id.navmap:
Intent intent = new Intent(MainActivityBuses.this, MapActivity.class);
startActivity(intent);
break;
case R.id.navhome:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment2()).commit();
break;
case R.id.navmybus:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment3()).commit();
break;
case R.id.navinfo:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment4()).commit();
break;
}
return true;
}
i got the answer,
just need to add this
btmNav.getMenu().findItem(R.id.navhome).setChecked(true);
below this
btmNav = findViewById(R.id.btmnav);
btmNav.setOnNavigationItemSelectedListener((navListner));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();
I'm working on this code
private void setupViews(){
frameLayout = (FrameLayout) findViewById(R.id.frame_id);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_id);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new ProfileFragment()).commit();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int navID = menuItem.getItemId();
switch (navID){
case R.id.home:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new HomeFragment()).commit();
break;
case R.id.search:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new SearchFragment()).commit();
break;
case R.id.profile:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new ProfileFragment()).commit();
break;
}
return true;
}
When I launch my app, it's automatically going on first bottom (I have 3 bottoms)
I want to change this to second bottom in navigation view. Please help me
Try this:
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_id);
bottomNavigationView.setSelectedItemId(R.id.search);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new SearchFragment()).commit();
#iamhanniballake
I call setupViews() in main activity
I have 3 botoms
-1-2-3-
when my app running open fragment with a bottomnav view
its automatically selected 3 I want to change it to 2
I am trying to implement a navigation drawer which switches among fragments and activities. Whenever I move to a activity window, the hamburger icon along with the navigation toolbar gets hidden which is why I cannot get back to my navigation drawer. How to resolve the issue?
This is my navigation code:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.nav_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
break;
case R.id.nav_my_events:
Intent intent = new Intent(getBaseContext(), Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
case R.id.nav_settings:
Toast.makeText(this,"Settings..", Toast.LENGTH_LONG).show();
break;
case R.id.nav_logout:
Toast.makeText(this,"You logged out..", Toast.LENGTH_LONG).show();
break;
}
I am working on and Android app, the main page has map fragment and some animations, when I click on options menu everything gets slow and paused for a second before the menu show up, and when I select an item from the menu same thing happens for a second until changes committed.
I use the menu to change Map type (standard, Satellite, Terrain)
Can I add a loading or progress bar before and after calling the menu options method everytime onOptionsItemSelected?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.first:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.second:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
}
return super.onOptionsItemSelected(item);
}
Try:
1. Remove MapStyleOptions styleOptions;
2. Clear map before changing type
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//Add menu handling code
switch (id) {
case R.id.first:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.second:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
}
return super.onOptionsItemSelected(item);
}
Let's say I have a ListFragment A that is rooted from MainActivity A. User presses a list from List A and go to the FragmentActivity B. FragmentActivity holds 3 tabs of fragments.
So, I want to put an up navigation to the FragmentActivity B, so that it goes back to ListFragment A. How do I go about that?
This is my try, so far no luck:
public class ItemDetailActivity extends FragmentActivity implements ActionBar.TabListener {
...
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
LatestFragment fragment = new LatestFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.pager, fragment).addToBackStack(null)
.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
LatestFragment is the ListFragment A I want to go back to.
However, I got an error that says I have to implement OnLatestSelectedListener because in LatestFragment, I already put an interface to pass values.
What else can I go inside onOptionsItemSelected?
Assuming you started ItemDetailActivity with a standard intent, you should just be able to use back action like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If fragment A is in your main activity then you can also look in to using ActionBar. Back button takes you to the main activity. It is quite easy to implement as well.
I assume your FragmentActivityB has got the back button.
if not you can get it by
getActionBar().setDisplayHomeAsUpEnabled(true);
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="#######" />
Now you can just identify fragments rooted with MainActivity using some flags like ViewNumber(1,2,3).
Implement the method onMenuItemSelected(....) and pass the viewNumber ie; the Fragment Which you want to show.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
//We are implementing this method to respond to the back button on the action bar.
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
Intent i = new Intent(this,MainActivity.class);
i.putExtra("viewNumber",2);
startActivity(i);
break;
}
return true;
}//onMenuItemSelected
In MainActivity get the viewNumber using Bundles, and you can have some method like this to work out and display the desired Fragment.
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 1:
fragment = new ListFragmentB();
break;
case 2:
fragment = new ListFragmentA();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
}
}
I hope it will solve your problem.