Creating Tabs Using Fragments Now That Tabactivity Is Deprecated

Creating tabs using Fragments now that TabActivity is deprecated

You will need to use the android compatibility library to use fragments. It can be found as a jar file in your android sdk folder.

There are many tutorials out there for fragments.

There is a project named ActionBarSherlock which demos all the capability of the compatibility library. Source code is also available on github. The link is on there site.

This is another tutorial
http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/

How to implement tabs now that TabActivity is deprecated

If you go to the Android developer guide they show you how this can be done

http://developer.android.com/reference/android/app/TabActivity.html

There is example code that shows you how to do this using fragments

How can I use fragments, now that TabActivity is deprecated?

In the Android docs you find an example of how to use a TabHost with Fragments. No TabActivity needed.
Here is the example: http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html

How to correctly create a Tablayout now that the Tabactivity is deprecated?

Since the release of ActionBarSherlock there is no need to use the old tap API. Just use the new tabs with fragments in any way you like on any recent Android Version (2.x)

Replacement of TabActivity with FragmentActivity and Fragments

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

The type TabActivity is deprecated For app tab

You can still use a deprecated package. It is however recommended to use Fragments, and thus the support package. You can read more about it here. However, if you are a beginner at java and android development, I would recommend ignoring the deprecation for now and come back to this when you have completed the tutorial you are currently using if you find it educating.

If you want to watch a nice example of tabbed navigation using Fragments, then create a new project in Eclipse using android 4.0 or later. Make sure your android-plugin is updated. You will get the option to create a project with basic navigation already implemented.



Related Topics



Leave a reply



Submit