0

I am trying to pass data between two activities of TabHost but not succeded. Here is my code:

public class AndroidTabLayoutActivity extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();

        TabSpec livedata = tabHost.newTabSpec("Photoes");
        livedata.setIndicator("Photoes", getResources().getDrawable(R.drawable.icon_photos_tab));
        Intent livedataIntent = new Intent(this, PhotosActivity.class);
        livedata.setContent(livedataIntent);

        TabSpec addedlegs = tabHost.newTabSpec("Songs");
        addedlegs.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
        Intent addedlegsIntent = new Intent(this, SongsActivity.class);
        addedlegs.setContent(addedlegsIntent);

        tabHost.addTab(livedata); 
        tabHost.addTab(addedlegs);         
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</TabHost>

output image: enter image description here I am performing some calculations in PhotosActivity.class(which is in first tab(Photoes)) activity and I would like to pass the result as string to the SongsActivity.class (which in the second tab(Songs)) where I use this string for further calculations.

Can anyone suggest a way to archieve that? Thanx inadvance!!

Parashuram
  • 303
  • 2
  • 6
  • 19

1 Answers1

0

The idea of the communication between the parent activity and its child is using the associated tags which is in your case are "Photoes" and "Songs".

For example: If you need to pass a string from the Tab "Photoes" to the parent activity.

1)you need to create a public method in the parent activity then call it as following from the child

((AndroidTabLayoutActivity)getParent()).myFunctionName("string value");

2)To pass this value to a specific child,e.g. "Songs", then you have to create a public method within this child activity and call it from the parent using it's Tag.

public void myFunctionName(String str){
    ActivityTab1 activity = (ActivityTab1) getLocalActivityManager().getActivity("Tab1");
    activity.takeThisValue(str);
}

You may also need to do some validation to make sure that the activity instances are not null

if(activity !=null)
//DO my stuff

Reference: https://androidactivity.wordpress.com/2012/08/17/two-way-communication-between-tabactivity-and-its-child-tabs/

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66