javaandroidfragmentmanagertabactivity

Program Structure for Tabs in Android


I am trying to build a simple app, that navigates via tabs (and further navigations via e.g. buttons). I drew a little scenario I want to build. Depiction of Scenario

enter image description here

For this I am using TabActivity and a FragmentManager. So, the compiler tells me already that TabActivity is deprecated. If this structure is deprecated please let me know and tell me what to use instead. Here is my code so far. It works until I press the Button. Nothing happens (Just the button disappears). I tried the add, hide, show methods of the FragmentManager already. Please let me know if I should put my entire code in here.

MainActivity.java

package com.example.tabstackoverflow;

import android.app.TabActivity;
import android.os.Bundle;

import android.widget.TabHost;

public class MainActivity extends TabActivity {

    HandleTabActivity tabAct;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_layout);
        tabAct = new HandleTabActivity();
        TabHost tabHost = tabAct.getTabActivity(this);
        tabHost.setCurrentTab(0);

    }
}

HandleTabActivity.java

package com.example.tabstackoverflow;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.widget.TabHost;

import com.example.tabstackoverflow.Tab1Activity;
import com.example.tabstackoverflow.Tab2Activity;

public class HandleTabActivity {
    HandleTabActivity() {

    }

    public TabHost getTabActivity (TabActivity curActivity) {
        Resources ressources = curActivity.getResources();
        TabHost tabHost = curActivity.getTabHost();

        //Tab 1
        Intent intentOwn = new Intent().setClass(curActivity, Tab1Activity.class);
        TabHost.TabSpec tabSpecOwn = tabHost
                .newTabSpec("Tab 1")
                .setIndicator("", ressources.getDrawable(R.drawable.pic_tab1))
                .setContent(intentOwn);

        //Tab 2
        Intent intentGet = new Intent().setClass(curActivity, Tab2Activity.class);
        TabHost.TabSpec tabSpecGet = tabHost
                .newTabSpec("Tab 2")
                .setIndicator("", ressources.getDrawable(R.drawable.pic_tab2))
                .setContent(intentGet);

        // add all tabs
        tabHost.addTab(tabSpecOwn);
        tabHost.addTab(tabSpecGet);
        return tabHost;
    }
}

Tab1Activity.java

package com.example.tabstackoverflow;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;


public class Tab1Activity extends AppCompatActivity implements Tab1Fragment1.SetViewToTab2 {

    private final String TAG = "<GetHelpTAG>";

    private Fragment fragment1;
    private Fragment fragment2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab1);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        fragment1 = Tab1Fragment1.newInstance();
        fragment2 = Tab1Fragment2.newInstance();

        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        transaction.replace(R.id.fragmentPlaceholder, fragment2);
        transaction.commit();

    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        if (fragment instanceof Tab1Fragment1) {
            Tab1Fragment1 getHelpSearchFragment = (Tab1Fragment1) fragment;
            getHelpSearchFragment.setListener(this);
        }
    }

    @Override
    public void onButtonClicked() {
        Toast.makeText(getBaseContext(),
                "OnClickListener !!!",
                Toast.LENGTH_SHORT).show();

        fragment2 = Tab1Fragment2.newInstance();

        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.show(fragment2);
        transaction.replace(R.id.fragmentPlaceholder, fragment2);

        transaction.commit();
    }

}

Tab1Fragment1.java

package com.example.tabstackoverflow;


import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class Tab1Fragment1 extends Fragment {

    private Button mSearchButton;
    private final String TAG = "<Tab1Fragment1TAG>";
    SetViewToTab2 callback;


    public static Tab1Fragment1 newInstance() {
        Bundle args = new Bundle();
        Tab1Fragment1 fragment = new Tab1Fragment1();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_tab1_view1, container, false);

        addListenerOnButton(v);
        return v;
    }

    public void addListenerOnButton(View v) {

        mSearchButton = v.findViewById(R.id.button);

        mSearchButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(TAG, "pushed");

                callback.onButtonClicked();

            }

        });
    }

    public void setListener (SetViewToTab2 callback) {
        this.callback = callback;
    }

    public interface SetViewToTab2 {
        public void onButtonClicked();

    }
}

activity_tab1.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Tab1Activity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </com.google.android.material.appbar.AppBarLayout>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.tabstackoverflow.Tab1Fragment1"
        android:id="@+id/fragmentPlaceholder"/>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

I will update my question as needed. Thanks in advice!


Solution

  • I found out that the tabs I have are actually nothing else then the Navigation. It is pretty simple to use with the provided NavController and there is even a UI to visualize the navigation of the app.