Posted By

uthopiko on 02/18/14


Tagged

android


Versions (?)

2 Option for Android Main


 / Published in: Java
 

2 option for onIconSelect

  1. @Override
  2. public void onIconSelect(String fragment) {
  3.  
  4. Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.content_layout);
  5.  
  6. Fragment newFragment = null;
  7.  
  8. newFragment = createFragment("fr.vogue.adresses.android." + fragment);
  9.  
  10. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  11.  
  12. if (currentFragment == null) {
  13. transaction.add(R.id.content_layout, newFragment);
  14. } else {
  15. transaction.remove(currentFragment).add(R.id.content_layout, newFragment);
  16. }
  17.  
  18. transaction.addToBackStack(null);
  19. transaction.commit();
  20.  
  21. }
  22.  
  23. private Fragment createFragment(String fragment) {
  24. Fragment newFragment = null;
  25. try {
  26. newFragment = (Fragment) Class.forName(fragment).newInstance();
  27. } catch (InstantiationException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. } catch (IllegalAccessException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. } catch (ClassNotFoundException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37. return newFragment;
  38. }

Report this snippet  

You need to login to post a comment.