Replacement of TabActivity with FragmentActivity and Fragments
Asked Answered
A

2

5

Since TabActivity is deprecated I need to find a way to do it with Fragments. I have worked with Fragments before I know how it works but I need a guide to create my tab host with FragmentActivities. I have found a couple of examples on the internet, and they are all about putting fragments into a container for the tabs.

What I want is to put FragmentActivities for each tab. Because in my application I will be using 4 tabs and each of the tabs have really complex content in it. As a result, I need to have a FragmentActivity for each tab to manage the Fragments that I will put under each tab within a separated container.

SOLUTION:

After answers and searching on internet I ended up with a Solution. According to the solution I have found, I keep track of Fragments in separated stack for each tab under tab host. Here is my GitHub repository, I created a small sample app for this problem.

Azelea answered 10/9, 2012 at 8:30 Comment(0)
T
9

Since TabActivity is deprecated I need to find a way to do it with Fragments.

If you don't want to use TabActivity - forget about putting FragmentActivities into tab's content.

I remind that you can use TabWidget without TabActivity. So you can try this solution:

  1. Create just one FragmentActivity.
  2. Put TabWidget into FragmentActivity's layout. Make TabWidget's content's height = 0.
  3. Under TabWidget in XML declare container for you Fragments (FrameLayout for example).
  4. In FragmentActivity just handle which tab is selected (TabHost.OnTabChangeListener) and put needed Fragment into container.
  5. Put programm logics (which was earlier in different activities) into different Fragments.

Or you can create FragmentActivity with TabWidget, and instead of switching Fragments you can directly put Fragments into each tab's content.

For example if you have 3 tabs and 3 fragments try what i do. Call showFragmentX when you need to change one fragment to another.

public class Test extends FragmentActivity {

private Fragment1 fragment1=new Fragment1();
private Fragment2 fragment2=new Fragment2();
private Fragment3 fragment3=new Fragment3();

private void showFragment1(){
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragments_container, fragment1);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

private void showFragment2(){
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragments_container, fragment2);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

private void showFragment3(){
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragments_container, fragment3);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.fragmentactivity_layout);
}
}

If you do so your fragmentX variables will not be deleted each time you put them in fragment_container. They will live while your FragmentActivity live. Take look at fragments lifecycle. Only OnCreateView and onDestroyView methods of fragments will call again and again while you replace one fragment to another.

Also Fragments has their onSaveInstanceState method where you can save the state of your fragment. For example: user typed his name in fragment1's editText. If you want to keep this data(name string) while user discover other fragments you should

1.save name string in fragment1's onSaveInstanceState method
2. into fragment1's onCreateView method check savedInstanceState bundle, if it's not null - fill edittext with string that you get from bundle.

public class Fragment1 extends Fragment {

EditText nameEditText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    View fragment1View=inflater.inflate(R.layout.fragment1_layout, container, false);
    nameEditText=(EditText) fragment1View.findViewById(R.id.edittext_name);

    //check for saved data. if it is not null - fill your edittext with saved string
    if(savedInstanceState!=null){
        String nameString=(String) savedInstanceState.get("nameString");
        nameEditText.setText(nameString);
    }
    return fragment1View;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //save data that user have entered
    outState.putString("nameString", nameEditText.getText().toString());
}

}

You can check data before saving. I hope my point is clear now.

Also if you call setRetainInstance(true) in onCreateView() method of your fragment - system will try to save the state of fragment (with all input data). link to description

Thence answered 10/9, 2012 at 9:8 Comment(8)
Thx for the answer, but there it comes another issue if I put fragments instead of activities into tabs. For example in the first tab, I want to replace the container with another fragment in the case user clicks a button, and then If I change the tab go to the second one this will replace the previous fragments. So in the case I go to first tab again it will create the fragment again and I will not be able to see the previous page. Do you have any idea how get rid of it ?Azelea
i've edited my answer. Take look on that. And also there is fragment examples in samples ("ApiDemos"). Check them out.Thence
Yeah I got it, but this is not the thing that I'm asking. I have worked with fragments and I know how it works. The thing that I dont know is that : Assume that we are in the first tab and I called another fragment in the case user clicks to the button, for second step I changed the tab to the second one, so here this will replace the data in it with the second tabs' fragment right ? But I also want to keep the thing have been changed in the first tab. So in this manner If I just replace the fragments onTabChange this will not keep the previously changed content in the previous tab. Any idea ?Azelea
ok, I seem to understand your problem. I've edited my answer one more time! check it out :)Thence
I will try it as soon as possible and let you if some thing happens. Thanks for the answer again :)Azelea
Thanks for the answer, I also made a small changes in the logic and it works in the way I want.Azelea
@Azelea can you please update the code with solution? or if you have placed it on a blog, please provide the link. thanksGrimonia
@QadirHussain Please see edits in my Question. You can find the sample app containing my solution. Hope It helps.Azelea
S
1

The approach of putting tab contents into separate Activities is deprecated, since now we have the Fragment framework. You should put the complex content of the tabs into Fragments, and swap those Fragments when the user selects tabs.

The recommended solution for this is to use the ActionBar with Tab navigation instead of using the TabHost widget.

Stravinsky answered 10/9, 2012 at 8:37 Comment(2)
Thanks for the answer, I have some concerns about using it. For example, If I put a fragment for the first tab, I will need a navigation within the same tab and as far as I know we are using just one container for whole tabs and the fragments in it, so After adding a couple fragments to main container within the first tab, I will change it to second tab, so when I do it, this will remove all the fragments that I have created and put in the container within the first tab and it will put the second fragment to the main container,Azelea
which means all data that I created in the first tab will be deleted and replaced with second fragment ? What if I come back to first tab ? I will be recreating it ?Azelea

© 2022 - 2024 — McMap. All rights reserved.