Replacing fragment on the fly

ant2009
Android Studio 0.5.8

Hello,

I have one activity (MainActivity) that will host 2 fragments (ListAddressBookFragment, AddAddressBookFragment) (only one at a time). The initial fragment will be the ListAddressBookFragment and will be inflated when MainActivity onCreate gets called.

/* MainActivity.java */
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /* Add and display fragment */
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.flFragmentContainer);

    /* Create new fragment if this hasn't already been done */
    if(fragment == null) {
        fragment = new ListAddressBookFragment();
        fragmentManager.beginTransaction()
                .add(R.id.flFragmentContainer, fragment)
                .commit();
    }
}

In the ListAddressBookFragment I have a option menu to add a new addressbook item. So this will call call MainActivity. So I want to replace ListAddressBookFragment with AddAddressBookFragment. However, because the code above is hardcoded I am wondering is there anyway to do this on the fly?

/* ListAddressBookFragment.java */
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();
    if (id == R.id.new_addressbook) {
        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Many thanks for any suggestions,

Swaroop

I would recommend you to read this article, Communicating with fragments

Basically, you need to call

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);

Also, consider using interface within fragment that activity implements.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replacing a substring with a link on the fly

From Dev

Fragment replacing in RecyclerView item

From Dev

Fragment replacing problems

From Dev

NullPointerException - Replacing fragment in FrameLayout

From Dev

Problems replacing an android fragment

From Dev

Replacing CardView with Fragment

From Dev

Android: Replacing a fragment in a view pager

From Dev

NullPointerException when replacing fragment android

From Dev

Replacing fragment does not work properly

From Dev

Android: Replacing a fragment in a view pager

From Dev

Replacing fragment in ViewPager causes IllegalStateException

From Java

Replacing css file on the fly (and apply the new style to the page)

From Dev

findViewById of TextSwitcher returns null after replacing fragment

From Dev

Circular reveal animation while replacing fragment

From Dev

Does replacing a fragment destroy all hidden fragments

From Dev

Android app crashes when reopening and replacing fragment

From Dev

Why is my Fragment not replacing my Activity layout?

From Dev

Action bar moving down while replacing with fragment

From Dev

Circular reveal animation while replacing fragment

From Dev

Replacing multiple fragmentclass using one fragment view

From Dev

Layout does not appear when replacing a fragment

From Dev

Replacing classes/resources with an OSGi fragment - possible without including a jar in the fragment?

From Dev

Replacing a fragment does not replace the previous fragment entirely. Why so?

From Dev

Replacing classes/resources with an OSGi fragment - possible without including a jar in the fragment?

From Dev

Replacing a fragment does not replace the previous fragment entirely. Why so?

From Dev

Replacing a fragment in a FrameLayout does not remove the existing fragment from the fragmentManager

From Dev

Android replacing fragment doesn't remove text field data

From Dev

Change fragment layout dynamically instead of replacing fragments Android SDK

From Dev

Lag when replacing ViewPager fragment in Navigation Bar over FrameLayout?

Related Related

  1. 1

    Replacing a substring with a link on the fly

  2. 2

    Fragment replacing in RecyclerView item

  3. 3

    Fragment replacing problems

  4. 4

    NullPointerException - Replacing fragment in FrameLayout

  5. 5

    Problems replacing an android fragment

  6. 6

    Replacing CardView with Fragment

  7. 7

    Android: Replacing a fragment in a view pager

  8. 8

    NullPointerException when replacing fragment android

  9. 9

    Replacing fragment does not work properly

  10. 10

    Android: Replacing a fragment in a view pager

  11. 11

    Replacing fragment in ViewPager causes IllegalStateException

  12. 12

    Replacing css file on the fly (and apply the new style to the page)

  13. 13

    findViewById of TextSwitcher returns null after replacing fragment

  14. 14

    Circular reveal animation while replacing fragment

  15. 15

    Does replacing a fragment destroy all hidden fragments

  16. 16

    Android app crashes when reopening and replacing fragment

  17. 17

    Why is my Fragment not replacing my Activity layout?

  18. 18

    Action bar moving down while replacing with fragment

  19. 19

    Circular reveal animation while replacing fragment

  20. 20

    Replacing multiple fragmentclass using one fragment view

  21. 21

    Layout does not appear when replacing a fragment

  22. 22

    Replacing classes/resources with an OSGi fragment - possible without including a jar in the fragment?

  23. 23

    Replacing a fragment does not replace the previous fragment entirely. Why so?

  24. 24

    Replacing classes/resources with an OSGi fragment - possible without including a jar in the fragment?

  25. 25

    Replacing a fragment does not replace the previous fragment entirely. Why so?

  26. 26

    Replacing a fragment in a FrameLayout does not remove the existing fragment from the fragmentManager

  27. 27

    Android replacing fragment doesn't remove text field data

  28. 28

    Change fragment layout dynamically instead of replacing fragments Android SDK

  29. 29

    Lag when replacing ViewPager fragment in Navigation Bar over FrameLayout?

HotTag

Archive