I created NavigationView and set 3 items.
As described in below picture :
Now what I want is, when I click paticular item, it should start an activity.
How to implement that?
Thanks.!!
What I can understand from your question is that you have used NavigationView for navigation drawer and you must have used menu file for your three menu option as displayed in the image you have shared. You need to handle those three cases in switch-case as given below. Change the menu item id according to your code and you are good to go.
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked())
menuItem.setChecked(false);
else
menuItem.setChecked(true);
//To close the menu drawer once it is clicked
drawerLayout.closeDrawers();
switch (menuItem.getItemId()){
case R.id.menu_item_appointments: //R.id.menu_item_appointment - menu item id which you mentioned in the menu file
// Code to start activity
return true;
case R.id.menu_item_upload_status:
//code to start activity
return true;
default:
Toast.makeText(getApplicationContext(),"Oops! Something went wrong.",Toast.LENGTH_SHORT).show();
return true;
}
}
});
I assumes that you are using listview in your Navigation drawer, which is a better way, now Check below code:
//to listen click events:
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
Here, position is the item no. of the navigation drawer item clicked. You can use a switch case to identify the item clicked and then call to startActivity using
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
Your selectItem should be like this:
private void selectItem(int position) {
Intent intent;
switch (position) {
case 0:
intent = new Intent(currentActivity, NewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
currentActivity.startActivity(intent);
break;
case 1: // and so on.
Try this
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_item_1:
menuItem.setChecked(true);
Intent intent1 = new Intent(this, Activity1.class)
startActivity(intent1);
return true;
case R.id.navigation_item_2:
menuItem.setChecked(true);
Intent intent2 = new Intent(this, Activity2.class)
startActivity(intent2);
return true;
case R.id.navigation_item_3:
menuItem.setChecked(true);
Intent intent3 = new Intent(this, Activity3.class)
startActivity(intent3);
return true;
case R.id.nav_sub_menu_item01:
menuItem.setChecked(true);
Intent intent = new Intent(this, Activity1.class)
startActivity(intent);
return true;
}
}
Create an OnNavigationItemSelected like the below example .
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_home) {
} else if (id == R.id.nav_project) {
Intent i = new Intent(HomeActivity.this, Project.class);
startActivity(i);
} else if (id == R.id.nav_gallery) {
Intent i = new Intent(HomeActivity.this, Gallery.class);
startActivity(i);
} else if (id == R.id.nav_contact) {
Intent i = new Intent(HomeActivity.this, Contact.class);
startActivity(i);
} else if (id == R.id.nav_login) {
Intent i = new Intent(HomeActivity.this, Login.class);
startActivity(i);
} else if (id == R.id.nav_favourites) {
Intent i = new Intent(HomeActivity.this, Favourite.class);
startActivity(i);
} else if (id == R.id.nav_share) {
}
}
add these two items in your strings.xml
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
Related
I am a new application developer.I use in my app drawer it has many options for the user.But sometimes I want to set a condition and hide an element from it.Sorry to ask the question again, but I tried to work on our previous posts and didn't work with me.
This is my code:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
navigationView.setNavigationItemSelectedListener(this);
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
drawer.closeDrawers();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
int id = item.getItemId();
if (id == R.id.nav_home) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
} else if (id == R.id.nav_Profile) {
Intent i = new Intent(this, UserProfile.class);
startActivity(i);
} else if (id == R.id.nav_Politics) {
Intent i = new Intent(this, Ploysity.class);
startActivity(i);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
So for example I want to hide nav_Politics how I can hide it?
Check out this snippet of code:
mDrawerLayout.closeDrawer(Gravity.LEFT);
mDrawerLayout.closeDrawer(Gravity.RIGHT, false);
Use Gravity.LEFT or GravityCompat.START to move the left drawer. Use Gravity.RIGHT or GravityCompat.END for the right.
false disable drawer closing animation.
I'm not an expert in Android, but really interested in fragments. Would be grateful for your help!
I've implemented a navigation drawer in my app. There is a base drawer activity and a number of fragments a user can switch between from the drawer menu. There is no problem if I want to move to another fragment, but it is working when I use an intent to start a new activity. If you look at my code below you'll see that I use two separate menus: one is for ActionBar icons (cart and search), where intents work perfectly and another one is for Nav drawer to jump between fragments. Is there any way to combine it in one? Basically, I need to move to Profile and Log out activities from nav drawer. Thank you for your advice in advance!
The screen is here
Java file
public class BaseDrawerActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private NavigationView nvDrawer;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_drawer);
drawerLayout = findViewById(R.id.drawer);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
nvDrawer = findViewById(R.id.nvgView);
// Setup drawer view
setupDrawerContent(nvDrawer);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Menu created for cart and search icons
#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, menu);
return super.onCreateOptionsMenu(menu);
//return true;
}
//This method is designed only for cart and search icons
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(actionBarDrawerToggle.onOptionsItemSelected(item)){
return true;
} else if (id == R.id.cart) {
Intent intent = new Intent(getApplicationContext(), CartActivity.class);
startActivity(intent);
} else if (id == R.id.search) {
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
//This method is to set up the drawer content/menu
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Here we create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_home:
fragmentClass = HomeFragment.class;
break;
case R.id.nav_cat:
fragmentClass = CatalogFragment.class;
break;
case R.id.nav_logout:
fragmentClass = CatalogFragment.class;
break;
case R.id.nav_login:
//what should I add here to be able to move to another activity (not a fragment). It doesn't work when I use intents
case R.id.nav_profile:
//need to move to another activity
default:
fragmentClass = HomeFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flcontent, fragment).addToBackStack("back tag").commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
drawerLayout.closeDrawers();
}
}
Have you tried doing the same thing you did with the optionItems and preventing the code related to the fragment to be executed when starting an Activity:
public class BaseDrawerActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private NavigationView nvDrawer;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_drawer);
drawerLayout = findViewById(R.id.drawer);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
nvDrawer = findViewById(R.id.nvgView);
// Setup drawer view
setupDrawerContent(nvDrawer);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Menu created for cart and search icons
#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, menu);
return super.onCreateOptionsMenu(menu);
//return true;
}
//This method is design only for cart and search icons
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(actionBarDrawerToggle.onOptionsItemSelected(item)){
return true;
} else if (id == R.id.cart) {
Intent intent = new Intent(getApplicationContext(), CartActivity.class);
startActivity(intent);
} else if (id == R.id.search) {
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
//This method is to set up the drawer content/menu
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Here we create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_home:
fragmentClass = HomeFragment.class;
break;
case R.id.nav_cat:
fragmentClass = CatalogFragment.class;
break;
case R.id.nav_logout:
fragmentClass = CatalogFragment.class;
break;
case R.id.nav_login:
Intent intent = new Intent(getApplicationContext(), OtherActivity.class);
startActivity(intent);
break;
case R.id.nav_profile:
Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);
startActivity(intent);
break;
default:
fragmentClass = HomeFragment.class;
}
//Code related to fragment should not execute when choosing to start an Activity
if(fragmentClass != null){
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flcontent, fragment).addToBackStack("back tag").commit();
// Highlight the selected item has been done by NavigationView
}
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
drawerLayout.closeDrawers();
}
}
here i used basic navigation view provided by android studio
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().getItem(1).setChecked(true);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
/* // Handle navigation view item clicks here.
int id = item.getItemId();*/
//Check and un-check menu item if they are checkable behaviour
switch (item.getItemId()) {
case R.id.nav_camera: {
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
startActivity(intent);
break;
}
case R.id.nav_gallery: {
Toast.makeText(getApplicationContext(), "youclicked", Toast.LENGTH_SHORT).show();
break;
}
/* else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}*/
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
even though i'm checking the second item it was responding to clicks how to stop that
please help me i'm facing this problem since yesterday
You can do this in onPrepareOptionsMenu(Menu menu)
#Override
public boolean onPrepareOptionsMenu (Menu menu){
super.onPrepareOptionsMenu(menu);
MenuItem myItem = menu.findItem(R.id.myId); //here your menu ids
myItem.setEnabled(false);
return true;
}
Prepare the Screen's standard options menu to be displayed. This is
called right before the menu is shown, every time it is shown. You can
use this method to efficiently enable/disable items or otherwise
dynamically modify the contents.
Comment these id's which one you don't want to click like this :-
case R.id.nav_camera: {
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
startActivity(intent);
break;
}
case R.id.nav_gallery: {
Toast.makeText(getApplicationContext(), "youclicked", Toast.LENGTH_SHORT).show();
break;
}
This way you can check the last item and the current item is the same or not
private int prevId = -1;
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (prevId == -1 || prevId != id) {
// Do event handling on each item.
}
// ...
}
Forgive my English :(
I have a problem with "NavigationDrawer". I have the Fragments "Home " that have no special activities, "Import, Gallery and SlideShow", all with activities running perfectly.
However, if I click on Import (HOME> IMPORT) the activity IMPORT opens, but if I click on (IMPORT> GALLERY / SLIDESHOW / HOME) the Import activity remains open.
I have to press the back button to go to HOME, to click on another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#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.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
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.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id){
case R.id.nav_home:
Intent h= new Intent(Home.this,Home.class);
startActivity(h);
break;
case R.id.nav_import:
Intent i= new Intent(Home.this,Import.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent g= new Intent(Home.this,Gallery.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s= new Intent(Home.this,Slideshow.class);
startActivity(s);
break;
// oh nightmare
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
IMPORT ACTIVITY
public class Import extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button Button9 = (Button) findViewById(R.id.Button9);
Button Button11 = (Button) findViewById(R.id.Button11);
//We dont need this.
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.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(Import.this, PHP5.class);
startActivity(it);
}
});
Button11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(Import.this, PHP7.class);
startActivity(it);
}
});
}
#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.home, 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.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id){
case R.id.nav_home:
Intent h= new Intent(Import.this,Home.class);
startActivity(h);
break;
case R.id.nav_import:
Intent i= new Intent(Import.this,Import.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent g= new Intent(Import.this,Gallery.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s= new Intent(Import.this,Slideshow.class);
startActivity(s);
break;
// after this lets start copying the above.
// FOLLOW MEEEEE>>>
//copy this now.
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Maybe you forgot to call finish() in the IMPORT Activity? If you call it after your Intent to another Activity, the current Activity will close, and your Home Activity will be shown again.
You need to remove all previous Activities from stack, it can be done trough a flag in the intent
Intent it = new Intent(Import.this, OtherAct.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(it);
Else a combination of the keywors may work for you:
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
Check the doc about how it works:
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK
I am using common Navigation drawer for all activities and fragments but when I am clicking on same item of Navigation drawer also it inflating the same screen. What I want is the same item should not be loaded for second time.
This is my code:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (id == R.id.nav_profile) {
Intent i = new Intent(MainFragmentsActivity.this, ProfileActivity.class);
startActivity(i);
drawer.closeDrawer(GravityCompat.START);
}
}
You should not use common navigation drawer for all activities and fragments. Common design pattern is showing different fragments on the main activity for the navigation drawer menu items. And if the user navigates inside any fragment you should open an activity without any navigation drawer but with a back arrow in action bar.
Now if you dont want to show the same fragment then in
onNavigationItemSelected you can write the logic like below -
Fragment fragment = getSupportFragmentManager().findFragmentByTag("TAG");
if (fragment == null)
{
Create and attach new fragment
}else{
//Do not do anything.
}
Simple thing, store clicked view'd id to a variable and compare if its the same,
int id; // use this variable to store id of clicked view.
#Override public boolean onNavigationItemSelected(MenuItem item) {
if(id != item.getItemId) {
id = item.getItemId(); // store clicked id in variable
// Do your clicking stuffs here...
if (id == R.id.nav_profile) {
Intent i = new Intent(MainFragmentsActivity.this, ProfileActivity.class);
startActivity(i);
drawer.closeDrawer(GravityCompat.START);
}
}
}
For fragments:
if (id == R.id.nav_things_to_do) {
if (id == R.id.nav_things_to_do && viewPager.getCurrentItem() != 0) {
Intent main1 = new Intent(getApplicationContext(), MainFragmentsActivity.class);
main1.putExtra("position", 0);
main1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
main1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
main1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(main1);
overridePendingTransition(0, 0);
drawer.closeDrawer(GravityCompat.START);
}
else {
drawer.closeDrawer(GravityCompat.START);
}
}
if (id == R.id.nav_all_tasks) {
if (id == R.id.nav_all_tasks && viewPager.getCurrentItem() != 1) {
Intent main2 = new Intent(getApplicationContext(), MainFragmentsActivity.class);
main2.putExtra("position", 1);
main2.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
main2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
main2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(main2);
overridePendingTransition(0, 0);
drawer.closeDrawer(GravityCompat.START);
}
else {
drawer.closeDrawer(GravityCompat.START);
}
}
For Activities:
if (id == R.id.nav_images) {
try {
Intent i = new Intent(MainFragmentsActivity.this,
SelectFilesActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
drawer.closeDrawer(GravityCompat.START);
} catch (Exception e) {
e.printStackTrace();
}
}