0

I am using Fragment, and My question is How to open the class Test1 and Test2 in Fragment. Actually I Using the Intent for redirect activity but I want to using fragment not Activity. How it is Possibile?

  I need to two tab. 1)App 2)Application. and Successfully Create both tab. but using with fragment Like TabHost. 

Please suggest me. My code Below,

My code is,

            import android.app.Fragment;
            import android.os.Bundle;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import android.widget.AnalogClock;
            import android.widget.TabHost;

            import com.example.app.R;

            public class CallFragment extends Fragment implements OnTabChangeListener{
                private TabHost tabHost = null;
                View rootView = null;
                TabHost.TabSpec spec, spec1;

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                    /**
                     * Inflate the layout for this fragment
                     */
                    View rootView = inflater.inflate(R.layout.call_fragment, container,
                            false);

                    tabHost = (TabHost) rootView.findViewById(R.id.tab_host);
                    tabHost.setup();
                    spec = tabHost.newTabSpec("tagApp");
                    spec1 = tabHost.newTabSpec("tagApplication");
                    spec.setIndicator("App");
                    spec.setContent(R.id.test1);

                    spec1.setIndicator("Application");
                    spec1.setContent(R.id.test2);

                    tabHost.setOnTabChangedListener(this);
                    tabHost.addTab(spec);
                    tabHost.addTab(spec1);
                    return rootView;
                }

        @Override
            public void onTabChanged(String tabId) {
                // TODO Auto-generated method stub
                if ("tagApp".equals(tabId)) {

                    Intent intent = new Intent(getActivity(), Test1.class);
                    getActivity().startActivity(intent);
                }
                if ("tagApplication".equals(tabId)) {
                    Intent intent = new Intent(getActivity(), Test2.class);
                    getActivity().startActivity(intent);
                }
            }
            }

    Test1.java file,
    public class Test1 extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test1);
        }
    }

    Test2.java file,
    public class Test2 extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test2);
        }
    }

    And call_fragment.xml file is,

        <?xml version="1.0" encoding="utf-8"?>
        <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tab_host"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" />

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                 <include layout="@layout/test1" />

                 <include layout="@layout/test2" />
            </FrameLayout>
            </LinearLayout>

        </TabHost>

    test1.xml file,
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/test1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tag 1" />

    </LinearLayout>

    test2.xml file,
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/test2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tag 2" />

    </LinearLayout>
Android
  • 8,995
  • 9
  • 67
  • 108
  • Your question is quite unclear imho. What I understood is: You want to start another activity (like a redirect) when a certain tab is selected/activated, correct? – MABVT Oct 29 '14 at 07:37
  • Yes, I want to redirect whenever click on selected tab. – Android Oct 29 '14 at 07:41
  • I an Using this Code, It is open New Activity But It Is open in Fragment then What using for it?@Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub if ("tagApp".equals(tabId)) { Intent intent = new Intent(getActivity(), Test1.class); getActivity().startActivity(intent); } if ("tagApplication".equals(tabId)) { Intent intent = new Intent(getActivity(), Test2.class); getActivity().startActivity(intent); } } – Android Oct 29 '14 at 09:46
  • Please post this code as an update cleanly in your post so that everybody can read it. – MABVT Oct 29 '14 at 09:56

1 Answers1

0

It is not possible to return an Intent if a return value of type "View" is expected. Approach this one differently.

For the redirecting tab, return an empty view return new View(/* whatever parameters you want to set */);.

Then register an onTabChangeListener in the TabHost, which then allows to react on tab-selections. For the redirecting tab index create and start your Intent causing the new Activity to be launched.

See this: How to use TabHost.OnTabChangeListener in android? for an existing example.

Community
  • 1
  • 1
MABVT
  • 1,350
  • 10
  • 17