Friday, May 18, 2018

Refreshing - Android fragments


Fragments are ways by which you can create multiple views in a single Activity, to avoiding to create multiple activities. You can add / remove fragments while the activity is running. 

You can create a fragment in 2 ways: - static using XML and dynamic using programmatically

XML: 

  • Create 2 fragment classes using the wizard. This will create a class that extends Fragment and create the layout xml accordingly. 
    • headerFragment
    • ArticleFragment. 
  • Now add the 2 fragments in the main activity XML file as below. 
<fragment
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/headline_fragment"
    android:layout_weight="1"
    android:name="io.ctrlspace.fragmentexample.HeadlineFragment"> </fragment>

 <fragment
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:id="@+id/article_fragment"
     android:layout_weight="1"
     android:name="io.ctrlspace.fragmentexample.ArticleFragment"> </fragment>


Now this is not of much use since we add these in static xml, often we may need to add dynamically fragments based on runtime logic. 


Programmatically: 

  • Create a simple Empty Activity - say MainActivity. 
  • In the main Activity layout file, add a layout to hold the fragments, say a frame layout 
  • Now create a Fragment using Android studio - call it as HomeFragment. This will create a Java class that extends Fragment and a fragment layout file. 
  • Add some content for the fragment in the layout design to identify that its a fragment content, say add a text box with a text inside first fragment. 
  • In the main Activity add the following code. 
    • Get the FragmentManager instance
    • Start the transaction of the fragment manager.
    • Add or replace the fragment using the add / replace method in the fragment transaction, pass the fragment container and the fragment to be added or replaced to this method. 
    • Commit the transaction. 

public static FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fragmentManager = getSupportFragmentManager();
    if(findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        HomeFragment homeFragment = new HomeFragment();
        fragmentTransaction.add(R.id.fragment_container, homeFragment, null);
        fragmentTransaction.commit();
    }
}

Fragment to Activity communication is handled in a different way - you can view the source code for this here. 


12 comments:

  1. For more information on Fragment to Activity communication visit this github link:
    https://github.com/deiveehan/android-samples-refresh/tree/master/fragmentactivitycommunication

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
    Data Science Training in Chennai | Data Science course in anna nagar
    Data Science course in chennai | Data science course in Bangalore
    Data Science course in marathahalli | Data Science course in btm

    ReplyDelete
  4. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    python training in pune | python training institute in chennai | python training in Bangalore

    ReplyDelete
  5. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    Oracle Training in Chennai | Best Oracle Training Institute in Chennai
    Web Design Training in Chennai
    Web Design Training in Chennai|Best Web Design Training in Chennai
    AngularJS Training in Chennai |Advanced SAS Training in Chennai | Best SAS Training in Chennai





    ReplyDelete

  6. This post is so interactive and informative.keep update more information...
    RPA Training in Velachery
    RPA Training in Chennai

    ReplyDelete