Navigate to Mainactivity from Navigation drawer - android

I want there to be a way to get back to the MainActivity from the nav when I switch fragments. The way I was trying to do it before was causing the app to crash. The main screen is the main activity.
public void selectDrawerItem(MenuItem menuItem)
{
Fragment fragment = null;
Class fragmentClass;
switch (menuItem.getItemId())
{
case R.id.nav_inspi:
fragmentClass = InFragment.class;
break;
default:
fragmentClass = InFragment.class;
break;
}
try
{
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e)
{
e.printStackTrace();
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fLayout_, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
zDrawerLayout.closeDrawers();
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
zDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
if (zDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}

Try this change in your code
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fLayout_, fragment).addToBackStack(null).commit();

Related

How to call a fragment every time you open the application?

I have a navigation drawer but the main activity has nothing, only the main page fragment has some thing, I want this fragment to be called every time the application opens. Anyone have any idea how to do this?
here is an example
As in code below, add this line in your code onNavigationItemSelected(nvDrawer.getMenu().getItem(0));
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...From section above...
// Find our drawer view
nvDrawer = (NavigationView) findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
selectDrawerItem(nvDrawer.getMenu().getItem(0));
}
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) {
// 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_first_fragment:
fragmentClass = FirstFragment.class;
break;
case R.id.nav_second_fragment:
fragmentClass = SecondFragment.class;
break;
case R.id.nav_third_fragment:
fragmentClass = ThirdFragment.class;
break;
default:
fragmentClass = FirstFragment.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).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
}

not re-create fragment in BottomNavigationView

Problem:
Each time the BottomNavigationView is clicked the fragment is re-created.
Currently in each fragment I have Recycler View that runs only once when creating the Fragment.
In this case, the RecyclerView data is loaded every time. And that's not what I'd like.
How could I work so that the Fragment remains active after it is clicked the first time. And when you go back to the Fragment already created, just display it without recreating it every time?
My attempts:
BottomNavigationView bottomNavigationView =(BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
final Menu menu = bottomNavigationView.getMenu();
fragmentManager = getSupportFragmentManager();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
/* Fragmento Start */
transaction.replace(R.id.content_home, new MainFragment()).commit();
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Intent in;
Fragment fragment = null;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.content_home);
switch (item.getItemId()){
case R.id.ic_home:
if (!(currentFragment instanceof MainFragment)) {
fragment = new MainFragment();
toolbar.setTitle("Main");
item = menu.getItem(0);
item.setChecked(true);
}
break;
case R.id.ic_categorias:
if (!(currentFragment instanceof CategoriasFragment)) {
fragment = new CategoriasFragment();
toolbar.setTitle("Categorias");
item = menu.getItem(1);
item.setChecked(true);
}
break;
case R.id.ic_calendar:
if (!(currentFragment instanceof FeedUserFragment)) {
fragment = new FeedUserFragment();
item = menu.getItem(2);
toolbar.setTitle("Feed");
item.setChecked(true);
}
break;
case R.id.ic_explore:
if (!(currentFragment instanceof ExploreFragment)) {
fragment = new ExploreFragment();
item = menu.getItem(3);
toolbar.setTitle("Explorar");
item.setChecked(true);
}
break;
case R.id.ic_person:
if (!(currentFragment instanceof Opcoes)) {
fragment = new Opcoes();
toolbar.setTitle("Opções");
item = menu.getItem(4);
item.setChecked(true);
}
break;
}
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.content_home, fragment);
transaction.commit();
return false;
}
});
You are replacing your fragment every time ie after your switch block
complete.
Solution: Replace only when condition meets and use addToBackStack which helps to maintain stacks. You can read more about from this site
Try this,
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Intent in;
Fragment fragment = null;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.content_home);
switch (item.getItemId()){
case R.id.ic_home:
if (!(currentFragment instanceof MainFragment)) {
fragment = new MainFragment();
toolbar.setTitle("Main");
item = menu.getItem(0);
item.setChecked(true);
loadFragment(fragment,"mainMenu");
}
break;
case R.id.ic_categorias:
if (!(currentFragment instanceof CategoriasFragment)) {
fragment = new CategoriasFragment();
toolbar.setTitle("Categorias");
item = menu.getItem(1);
item.setChecked(true);
loadFragment(fragment,"categories");
}
break;
case R.id.ic_calendar:
if (!(currentFragment instanceof FeedUserFragment)) {
fragment = new FeedUserFragment();
item = menu.getItem(2);
toolbar.setTitle("Feed");
item.setChecked(true);
loadFragment(fragment,"feedUsers");
}
break;
case R.id.ic_explore:
if (!(currentFragment instanceof ExploreFragment)) {
fragment = new ExploreFragment();
item = menu.getItem(3);
toolbar.setTitle("Explorar");
item.setChecked(true);
loadFragment(fragment,"explore");
}
break;
case R.id.ic_person:
if (!(currentFragment instanceof Opcoes)) {
fragment = new Opcoes();
toolbar.setTitle("Opções");
item = menu.getItem(4);
item.setChecked(true);
loadFragment(fragment,"opcoes");
}
break;
}
return false;
}
});
/**
* Create new method which replace your old fragment
*/
public void loadFragment(Fragment fragment, String tag){
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.content_home, fragment);
transaction.addToBackStack(tag);
transaction.commit();
}
I would try transaction.add() and search for fragments in back stack with this method findFragmentByTag. https://developer.android.com/reference/android/support/v4/app/FragmentManager.html#findFragmentByTag(java.lang.String).

How to limit tap on BottomNavigatioView item to only once, to not reload fragment?

How can I limit a tap on a BottomNavigatioView item to just once, so that my mapfragment doesn't have to reload itself when someone spamms the item?
My current Solution is this one:
fragmentManager = getSupportFragmentManager();
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
Fragment currentFragment = fragmentManager.findFragmentById(R.id.main_container);
int id = item.getItemId();
switch (id){
case R.id.World:
if (!(currentFragment instanceof MapsFragment)) {
fragment = new MapsFragment();
}
break;
case R.id.Friends:
fragment = new FriendsFragment();
break;
case R.id.Chats:
fragment = new You_Fragment();
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
return true;
}
});
but it doesn't work since the fragment throws a Nullpointer.
What can i change to achieve the above?
Try this! Worked for me:
fragmentManager = getSupportFragmentManager();
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.main_container);
int id = item.getItemId();
switch (id){
case R.id.World:
if (currentFragment instanceof MapsFragment) {
return false;
} else {
fragment = new MapsFragment();
}
break;
case R.id.Friends:
fragment = new FriendsFragment();
break;
case R.id.Chats:
fragment = new You_Fragment();
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
return true;
}
});

Switch Fragment(Show/Hide) with bottom bar Android

I have problem with implement fragment and bottom bar on my apps..
I wan to switch fragment according initialize bottom bar..
this my code
private Fragment frag_summary = new SummaryFragment();
private Fragment frag_my_proposal = new MyProposalFragment();
private Fragment frag_proposal = new ProposalFragment();
private Fragment frag_friends = new FriendsFragment();
bottomNavigation.setOnTabSelectListener(new OnTabSelectListener() {
#Override
public void onTabSelected(#IdRes int tabId) {
setSelectedFragment(tabId);
}
});
public void setSelectedFragment(int id) {
switch (id) {
case R.id.menu_home:
setFragment(id, frag_summary);
break;
case R.id.menu_my_proposals:
setFragment(id, new MyProposalFragment());
break;
case R.id.menu_proposal:
setFragment(id, new ProposalFragment());
break;
case R.id.menu_friends:
setFragment(id, new FriendsFragment());
break;
case R.id.menu_more:
/* drawer.openDrawer(GravityCompat.START);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}*/
break;
default:
setFragment(id, frag_summary);
break;
}
}
private void setFragment(int id, Fragment fragments) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
String fragmentTag = String.valueOf(id);
if (frag_summary.isAdded()) {
fragmentTransaction.hide(frag_summary);
}
if (frag_my_proposal.isAdded()) {
fragmentTransaction.hide(frag_my_proposal);
}
if (frag_proposal.isAdded()) {
fragmentTransaction.hide(frag_proposal);
}
if (frag_friends.isAdded()) {
fragmentTransaction.hide(frag_friends);
}
if (fragments.isAdded()) {
fragmentTransaction.show(fragments);
} else {
fragmentTransaction.add(R.id.content_frame, fragments, fragmentTag);
}
fragmentTransaction.commitAllowingStateLoss();
}
The Issue is : When I selected from Fragment frag_summary to Fragment frag_friends, Fragment frag_summary and other fragment sometimes still showed.. like other activity ex: from Fragment frag_friends to Fragment frag_proposal .
sho how to fix it ?
sorry for my english
Repalce the code in setFragment with -
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragments, fragmentTag);
fragmentTransaction.commitAllowingStateLoss();
Basically you are replacing the current visivble fragment.
Edit : Change the code as below -
public void setSelectedFragment(int id) {
switch (id) {
case R.id.menu_home:
setFragment(id, frag_summary);
break;
case R.id.menu_my_proposals:
setFragment(id, /*send the class instance you created for this fragment */);
break;
case R.id.menu_proposal:
setFragment(id, /*send the class instance you created for this fragment */);
break;
case R.id.menu_friends:
setFragment(id, /*send the class instance you created for this fragment */);
break;
case R.id.menu_more:
/* drawer.openDrawer(GravityCompat.START);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}*/
break;
default:
setFragment(id, frag_summary);
break;
}
}

Actionbar launch fragment

How can I launch a fragment from onOptionsItemsSelected?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_support:
Fragment f = new SupportFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(android.R.id.content, f).commit();
Toast display = Toast.makeText(this, "Settings", 10);
display.show();
case R.id.action_guide:
Toast display1 = Toast.makeText(this, "Guide", 10);
display1.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Im trying to launch the support fragment.
Thanks
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.action_support:
fragment = new Home();
case 1:
fragment = new NotesList();
default:
fragment = new defaultPageOrAnyOtheOption();
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
return true;
} // onOptionsItemSelected
Try it
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_support:
//launch Support Fragment
// Fragment f = new SupportFragment();
// replace(android.R.id.content, f);
Toast display = Toast.makeText(this, "Settings", 10);
display.show();
return true;
case R.id.action_guide:
Toast display1 = Toast.makeText(this, "Guide", 10);
display1.show();
return true;
}
}

Categories

Resources