Load an Activity inside a Fragment with tabHost

Asked

Viewed 1,073 times

0

I’m starting with the android studio reading some things and watching some videos on youtube, basically what I intend to do is a calculation program to make my life easier in my service, some simple accounts I have already managed to do (I only got the calculations), but with some bad layouts.

I found that tabHost slide with fragments, and I really liked the result, simple and practical.

I then created from a tutorial this app below that creates tabs, and when I click on each one (or use the "slide") it shows me the ID of the tab, but what I would need is instead of just showing a text, load another activity and maintain the tab for easy access to others.

I put a IF to compare whether the ID tab for "such", loads "such" activity, until then it worked, but in this activity was missing tabs, so I copied all the tabs code from the first activity for this second, and by clicking on the second tab, I see that it carries the new activity (just the text, nothing of the colors and layout) but soon after hangs the app.

I honestly didn’t understand what I did wrong, I even tried to put one FOR and a WHILE to ensure that she would charge only once (I thought I was stuck loading the second activity several times) but nothing has changed....

Would someone please have some light ?

activity_main.xml

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >




<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/horizontalScrollView"
        android:scrollbars="none"
        android:scrollbarSize="0dp">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"/>
</HorizontalScrollView>

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

</LinearLayout>

Mainactivity.java

package com.example.tbarata.myapplication;
import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost;

public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost;

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

    setContentView(R.layout.activity_main);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Tab 1111", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Tab 2222", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Tab 3333", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab44").setIndicator("Tab 44", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab5").setIndicator("Tab 55", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab6").setIndicator("Tab 66", null),
            FragmentTab.class, null);
}
}

activity_fragment_layout.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#eaecee">

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/hello_world"
    android:textAppearance="?android:attr/textAppearanceMedium" />

Fragmenttab.java

package com.example.tbarata.myapplication;
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;

public class FragmentTab extends Fragment {

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_fragment_layout, container, false);
     if (this.getTag() == "tab2") {

            Intent intent = new Intent(getActivity(), fragment_layout2.class);
            startActivity(intent);
                        }
    } else {

        TextView tv = (TextView) v.findViewById(R.id.text);
        tv.setText(this.getTag() + " Content");
    }
    return v;
}
}

and the fragment_layout2.java

public class fragment_layout2 extends FragmentActivity {
private FragmentTabHost mTabHost;

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

    setContentView(R.layout.activity_fragment_layout2);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Tab 1111", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Tab 2222", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Tab 3333", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab44").setIndicator("Tab 44", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab5").setIndicator("Tab 55", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab6").setIndicator("Tab 66", null),
            FragmentTab.class, null);

}
  • 1

    Android development evolves very quickly. Tabhost is considered deprecated, instead began to use Actionbar tabs that, with the emergence of Android 5, also passed the deprecated. If you are starting now avoid using things that are no longer used. At this time the way to implement tabs is using Tablayout, see here as.

  • Thank you @ramaral ! , today I’m a little tied up, but tomorrow I’ll start studying Tablayout, thank you very much for the tip! I went to "embalo" to see several tabHost tutorials, I did not see anything about being obsolete or not.... alias, I often ignored that same mention of Androidstudio because I don’t know these points a little bit. Thank you very much!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.