Android fragment! I have a question!

Asked 2 years ago, Updated 2 years ago, 75 views

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, FragmentCallback{

navFragment1 navFrag1;
navFragment2 navFrag2;
navFragment3 navFrag3;
ViewPager viewPager;
Toolbar toolbar;
MovieFragment fragment4;

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


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    navFrag1 = new navFragment1();
    navFrag2 = new navFragment2();
    navFrag3 = new navFragment3();
    viewPager = new ViewPager();
    fragment4 = new MovieFragment();

    getSupportFragmentManager().beginTransaction().add(R.id.container, viewPager).commit();


}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } } else {
        super.onBackPressed();
    }
}

@Override
public void onFragmentChanged(int index) {
    Fragment curFragment = null;

    if (index == 0) {
        curFragment = navFrag1;
        toolbar.setTitle ("first navigation");
    } } else if (index == 1) {
        curFragment = navFrag2;
        toolbar.setTitle ("Second Nav");
    } } else if (index == 2) {
        curFragment = navFrag3;
        toolbar.setTitle ("third navigation");
    } } else if (index == 3) {
        curFragment = fragment4;
    }

    getSupportFragmentManager().beginTransaction().replace(R.id.container, curFragment).addToBackStack(null).commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.nav_0) {
        onFragmentChanged(0);
    } } else if (id == R.id.nav_1) {
        onFragmentChanged(1);
    } } else if (id == R.id.nav_2) {
        onFragmentChanged(2);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

=================================================================================

public class ViewPager extends Fragment{


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.viewpager1, container, false);

    Button button = rootView.findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });


    return rootView;
}
}

Click Button in Viewpager fragment to change to MovieFragment How should I approach this? Also, pressing the back button on navFragment will shut down the app....

fragment android

2022-09-22 18:49

1 Answers

Well, it's a little strange that once addToBackStack() is applied, but if you press backkey with the navFragment, the app will not return to ViewPager fragment; I think you can handle the change to MovieFragment as below.

Create a method to replace with MovieFragment in Activity

public void replaceMoveFragment(){
    getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment4).addToBackStack(null).commit();
}

OnClick() in ViewPager fragment

Button.setOnClickListener(newView).OnClickListener() {
    @Override
    public void onClick(View v) {
        ((MainActivity)getActivity()).replaceMovieFragment();
    }
});


2022-09-22 18:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.