Monday, 18 February 2013

Nested Fragments

Previously we have learned some basics about using Fragments inside activtiy.

Fragments are very useful. However until recently fragments could not be used inside other fragment. That is you Activty can have multiple fragments ,but fragments cannot have fragments inside them.

With Android 4.2 APIv17 there was support for Nested Fragments Where you can use fragment inside other fragment. However there is a limitation that your fragment must be dynamic. There is no option to use <fragment> tags.

Here's a demo of using fragment inside fragment.

We will make use of our sample that we have been working in previous posts. We will change FragmentA. Here's a new Layout for FragmentA.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I am FragmentA" />

<FrameLayout
        android:id="@+id/child_fragment"
        android:layout_width="fill_parent"
        android:layout_height="200dip" />

</LinearLayout>

We use FrameLayout wherever we want our fragment to be . And so the updated FragmentA will be

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
public class FragmentA extends Fragment {
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      View view = (View) inflater.inflate(R.layout."name of layout",
    container, false);
      return view ;
   }
}

Now the child Fragment let it be FragmentC
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
public class FragmentC extends Fragment {
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      TextView textView=new TextView(getActivity());
      textView.setText("Hello I am fragment C");
      return textView;
   }
}
And to add it dynamically to your FragmentA
Fragment fragmentC = new FragmentC();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.child_fragment, fragmentC ).commit();
Use getParentFragment() to get the reference to parent similar to getActivity that gives reference of parent Activtiy.

15 comments:

  1. Nice article love to read the whole new way of your writing this things.

    Haidee
    www.gofastek.com

    ReplyDelete
  2. otr214427
    INTERNATIONAL CONCEPT OF WORK FROM HOME
    Work from home theory is fast gaining popularity because of the freedom and flexibility that comes with it. Since one is not bound by fixed working hours, they can schedule their work at the time when they feel most productive and convenient to them. Women & Men benefit a lot from this concept of work since they can balance their home and work perfectly. People mostly find that in this situation, their productivity is higher and stress levels lower. Those who like isolation and a tranquil work environment also tend to prefer this way of working. Today, with the kind of communication networks available, millions of people worldwide are considering this option.

    Women & Men who want to be independent but cannot afford to leave their responsibilities at home aside will benefit a lot from this concept of work. It makes it easier to maintain a healthy balance between home and work. The family doesn't get neglected and you can get your work done too. You can thus effectively juggle home responsibilities with your career. Working from home is definitely a viable option but it also needs a lot of hard work and discipline. You have to make a time schedule for yourself and stick to it. There will be a time frame of course for any job you take up and you have to fulfill that project within that time frame.

    There are many things that can be done working from home. A few of them is listed below that will give you a general idea about the benefits of this concept.

    Baby-sitting
    This is the most common and highly preferred job that Women & Men like doing. Since in today's competitive world both the parents have to work they need a secure place to leave behind their children who will take care of them and parents can also relax without being worried all the time. In this job you don't require any degree or qualifications. You only have to know how to take care of children. Parents are happy to pay handsome salary and you can also earn a lot without putting too much of an effort.

    Nursery
    For those who have a garden or an open space at your disposal and are also interested in gardening can go for this method of earning money. If given proper time and efforts nursery business can flourish very well and you will earn handsomely. But just as all jobs establishing it will be a bit difficult but the end results are outstanding.

    Freelance
    Freelance can be in different wings. Either you can be a freelance reporter or a freelance photographer. You can also do designing or be in the advertising field doing project on your own. Being independent and working independently will depend on your field of work and the availability of its worth in the market. If you like doing jewellery designing you can do that at home totally independently. You can also work on freelancing as a marketing executive working from home. Wanna know more, email us on workfromhome.otr214427@gmail.com and we will send you information on how you can actually work as a marketing freelancer.


    Internet related work
    This is a very vast field and here sky is the limit. All you need is a computer and Internet facility. Whatever field you are into work at home is perfect match in the software field. You can match your time according to your convenience and complete whatever projects you get. To learn more about how to work from home, contact us today on workfromhome.otr214427@gmail.comand our team will get you started on some excellent work from home projects.


    Diet food
    Since now a days Women & Men are more conscious of the food that they eat hence they prefer to have homemade low cal food and if you can start supplying low cal food to various offices then it will be a very good source of income and not too much of efforts. You can hire a few ladies who will help you out and this can be a good business.

    Thus think over this concept and go ahead.

    ReplyDelete
  3. I understand that every person has the passion in any aspects or things. If you love something and it came in front of you it completes your day and your mood turns into something you won't expected. I love your work and I want to read more about it. Visit my site if you have time . Thank you.

    triciajoy.com

    www.triciajoy.com

    ReplyDelete
  4. Love it! Very interesting topics, I hope the incoming comments and suggestion are equally positive. Thank you for sharing this information that is actually helpful.


    ufgop.org
    ufgop.org

    ReplyDelete
  5. hi frnds please tell me....
    How to call one fragment to another fragment from BaseAdapter class onClickListener and pass datas...

    ReplyDelete
  6. hi frnds please tell me....
    How to call one fragment to another fragment from BaseAdapter class onClickListener and pass datas...

    ReplyDelete
  7. Nice Article.. Thank you for the information..

    ReplyDelete
  8. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in android programming your post help me a lot.Thanks for your informative article. Android Training in velachery | Android Training institute in chennai

    ReplyDelete
  9. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Android Training in chennai | Android Training

    ReplyDelete
  10. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Mobile App Development Company in Chennai
    Android app Development Company in Chennai
    ios app development Company in Chennai

    ReplyDelete
  11. DUDEEE!!! THANKS A BUNCH!!! Life saviour!!

    ReplyDelete
  12. Thanks a lot.We will you have to share this blog content information increase with my knowledge learning skills.We develop to that's our info creative was really good.
    devops training
    devops online training
    devops training institute
    top 10 devops online training institute

    ReplyDelete