0

I have this method that starts a new activity and it works perfectly, but how would I write something similar for opening a fragment?

public void goToInformatica(View v) {
    Intent a = new Intent(this, i.class);
    startActivity(a);

usecase;

I have this hamburgermenu that takes me to different fragments, but I want some onscreen buttons to take me to the same fragments. The ID's for the buttons in the menu and the buttons onscreen are the same but because I call the fragments form OnNavigationItemSelected this doesn't work for normal buttons outside of the menu.

Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17
d n
  • 15
  • 5
  • 2
    Possible duplicate of [Open Fragment From Activity](https://stackoverflow.com/questions/36583205/open-fragment-from-activity) – Abraham Mathew May 27 '19 at 09:55

1 Answers1

0

you can use below code

  protected void setFragment(Fragment fragment, String tag) {
        FragmentTransaction t = getFragmentManager().beginTransaction();
        t.replace(R.id.container_dashboard, fragment, tag);
        t.commit();
    }

getSupportFragmentManager() if you are doing fragment transaction from activity

getFragmentManager() if you are doing fragment transaction within fragment

akshay_shahane
  • 4,423
  • 2
  • 17
  • 30