javaandroidandroid-activitytabbed

include an activity inside a tab result in full screen activity


tabbed activity overrides by activity when follow android tabbed activity.

I am a newbie in android. I am learning to include some activities on different tabs. however, the activity gets fullscreen and override the main tab

// TabMain.java 
public class TabMain extends AppCompatActivity {

  .......
}
// SectionsPagerAdapter.java extends FragmentPagerAdapter
@Override
    public Fragment getItem(int position) {
        switch (position){
            case 1:
                FragmentTab1 tab1 = new FragmentTab1();
                return tab1;
                default:
                    return PlaceholderFragment.newInstance(position + 1);
        }
    }
public class FragmentTab1 extends Fragment {

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.galery_main_layout, container, false);
        Intent intent = new Intent(getActivity(), UploadMain.class);
        getActivity().startActivity(intent);

        return root;
    }
}

the UploadMain.java implement Activity

<application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity
            android:name=".TabMain"
            android:label="@string/title_activity_tab_main"
            android:theme="@style/AppTheme.NoActionBar">

        </activity>
        <activity
            android:name=".FdActivity"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".UploadMain"
            android:configChanges="keyboardHidden|orientation"
            >

        </activity>
    </application>

I am expecting for the upload main class to be inside tab2

TabMain | Tab 1 (other activity) | Tab 2 (UploadMain)


Solution

  • You can't have a Activity inside a ViewPager, it must be a fragment.

    And also, when you start activity, like this,startActivity(intent) it will not replace the previous screen or activity, it will just create a new activity with a new layout, having the previous activity paused (and also sometimes stopped) until you close the previous activity. You need to learn more about activity and fragment lifecycles