marwan
marwan

Reputation: 115

How to move from Bottom Navigation Bar to activity android

I add to my app Bottom Navigation Bar it's work but the problem now I want move from one button in Bottom Navigation Bar to activity that activity have to fragment to view (TabLayout,ViewPager). When I try to make it like that selectedFragment = new MainActivityOderLIstFargmant(); the show me error like that .. and also like that and also not work.. startActivity(new Intent(NafMain.this, MainActivityOderLIstFargmant.class));

enter image description here

I need to move to other activity to show two (TabLayout,ViewPager).

this activity I want to move to it ..

public class MainActivityOderLIstFargmant extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;
    PageAdapterOrderList pageAdapterOrderList;
    TabItem tabChats;
    TabItem tabCalls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mani_order_list);
        tabLayout = findViewById(R.id.tablayout);
        tabChats = findViewById(R.id.tabChats);
        tabCalls = findViewById(R.id.tabCalls);
        viewPager = findViewById(R.id.viewPager);

        pageAdapterOrderList = new PageAdapterOrderList(getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(pageAdapterOrderList);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    }
}


from here

public class NafMaintest extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_naf_main);
        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
            BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
            bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = null;
                    switch (menuItem.getItemId()) {
                        case R.id.nav_home:
                           selectedFragment = new MainActivityOderLIstFargmant();//I try like that but not work 
                            break;
                        case R.id.nav_favorites:
                            selectedFragment = new FragmentHome();
                            break;
                        case R.id.nav_search:
                            selectedFragment = new FragmentProfile();
                            break;
                        case R.id.nav_s:
                            selectedFragment = new MainActivity();
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                            selectedFragment).commit();
                    return true;

                }
            });

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new FragmentHome()).commit();
        }
    }



}



If anyone know how solution it help me

Upvotes: 1

Views: 2299

Answers (1)

Zain
Zain

Reputation: 40888

you are trying to instantiate MainActivityOderLIstFargmant activity in the switch case R.id.nav_home and put it into a Fragment variable which is called selectedFragment, so both types are different (Fragment & Activity). That is why the screenshot you provided says Incompatible types.

To get your code works you can change the switch case as below

case R.id.nav_home:
    Intent intent = new Intent(NafMaintest.this, MainActivityOderLIstFargmant.class);
    startActivity(intent);
    return true; // use return to avoid triggering the fragment transaction after the `swtich` block

Upvotes: 1

Related Questions