Search code examples
androidnavigationfragment-tab-host

Call an activity from a listViewItem Without losing the tabNavigation


I'm noob on android development, and im trying to do a simple app, for details about certain products and stuff.

i have the the following layout:

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+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"
            >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0"/>

            <FrameLayout
                android:id="@+android:id/realtabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

            <TabWidget
                android:id="@android:id/tabs"
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                />
        </LinearLayout>
</android.support.v4.app.FragmentTabHost>

There are 2 tabs on that tabHost navigation, each one extends fragment like this

package co.com.smartmedia.vademecumtg;

import co.com.smartmedia.vademecumtg.R;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class LineasTab extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        return inflater.inflate(R.layout.tab2, container, false);
    }
}

In the first tab of that navigation i have a listView, and each item of that list, must call another activity that will show details about it.

im calling a new activity by calling this method from an itemClickListener:

public void intentForMedView(int pos){
        Log.w("debbuging", "start");
        Intent intent = new Intent(getActivity().getApplicationContext(), MedViewActivity.class);
        intent.putExtra(EXTRA_MEDTITLE, array_sort.get(pos).getNombre());
        intent.putExtra(EXTRA_MEDCONTENT, array_sort.get(pos).getLinea());
        Log.w("debbuging", "send intent"+intent);
        startActivity(intent);
    }

and the activity that i'm calling is the following:

package co.com.smartmedia.vademecumtg;

import co.com.smartmedia.vademecumtg.R;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MedViewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_med_view);

        Intent intent = getIntent();
        String title = intent.getStringExtra(MedicamentosTab.EXTRA_MEDTITLE);
        setTitle(title);

        TextView text = (TextView) findViewById(R.id.text);
        String lineName = intent.getStringExtra(MedicamentosTab.EXTRA_MEDCONTENT);
        text.setText(lineName);


    }
}

My problem is:

When the detail activity is shown, my navigation Disappears (the tabHost navigation), and i don't want it....

some tips or ways to do it ?

thaks...


Solution

  • I got the answer now, The way to accomplish this architecture, is to use FragmentTransaction

    http://developer.android.com/guide/components/fragments.html

    Cause it isn't necessary another activity... just play with the fragments of the UI.

    FragmentManager fragmentManager = getFragmentManager()
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    

    And take care with the android version cause android.support.v4.app.FragmentManager cannot cast to the android.app.FragmentManager

    Hope this be usefull