SHARE
TWEET

MainActivity

a guest Aug 18th, 2019 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.moviecatalogue;
  2.  
  3. import android.content.Context;
  4. import android.support.annotation.NonNull;
  5. import android.support.design.widget.BottomNavigationView;
  6. import android.support.v4.app.Fragment;
  7. import android.support.v4.app.FragmentTransaction;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.MenuItem;
  11.  
  12. import com.example.moviecatalogue.View.MovieFragment;
  13. import com.example.moviecatalogue.View.SettingFragment;
  14. import com.example.moviecatalogue.View.TvShowFragment;
  15.  
  16. import static com.example.moviecatalogue.ViewModel.MainViewModel.KEY_MOVIES;
  17.  
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20.  
  21.     private Fragment pageContent = new MovieFragment();
  22.     private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
  23.         = new BottomNavigationView.OnNavigationItemSelectedListener() {
  24.         @Override
  25.         public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
  26.             switch (menuItem.getItemId()) {
  27.                 case R.id.movie_nav:
  28.                     //Fragment Initialization
  29.                     MovieFragment movieFragment = new MovieFragment();
  30.                     //Fragment Transaction
  31.                     FragmentTransaction fragmentMovieTransaction = getSupportFragmentManager().beginTransaction();
  32.                     fragmentMovieTransaction.replace(R.id.fragment_container, movieFragment, movieFragment.getClass().getSimpleName());
  33.                     fragmentMovieTransaction.commit();
  34.                     return true;
  35.                 case R.id.tvshow_nav:
  36.                     //Fragment Initialization
  37.                     TvShowFragment tvShowFragment = new TvShowFragment();
  38.                     //Fragment Transaction
  39.                     FragmentTransaction fragmentTvShowTransaction = getSupportFragmentManager().beginTransaction();
  40.                     fragmentTvShowTransaction.replace(R.id.fragment_container, tvShowFragment, tvShowFragment.getClass().getSimpleName());
  41.                     fragmentTvShowTransaction.commit();
  42.                     return true;
  43.                 case R.id.setting_nav:
  44.                     //Fragment Initialization
  45.                     SettingFragment settingFragment = new SettingFragment();
  46.                     //Fragment Transaction
  47.                     FragmentTransaction fragmentsettingTransaction = getSupportFragmentManager().beginTransaction();
  48.                     fragmentsettingTransaction.replace(R.id.fragment_container, settingFragment);
  49.                     fragmentsettingTransaction.commit();
  50.                     return true;
  51.             }
  52.             return false;
  53.  
  54.  
  55.         }
  56.  
  57.     };
  58.  
  59.     @Override
  60.     protected void onCreate(Bundle savedInstanceState) {
  61.         super.onCreate(savedInstanceState);
  62.         setContentView(R.layout.activity_main);
  63.  
  64.         MovieFragment movieFragment = new MovieFragment();
  65.  
  66.         FragmentTransaction fragmentMvTransaction = getSupportFragmentManager().beginTransaction();
  67.         fragmentMvTransaction.replace(R.id.fragment_container, movieFragment, movieFragment.getClass().getSimpleName());
  68.         fragmentMvTransaction.commit();
  69.  
  70.         BottomNavigationView bottomNav = findViewById(R.id.nav_view);
  71.         bottomNav.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
  72.  
  73.         if (savedInstanceState == null) {
  74.             bottomNav.setSelectedItemId(R.id.movie_nav);
  75.         } else {
  76.             pageContent = getSupportFragmentManager().getFragment(savedInstanceState, KEY_MOVIES);
  77.  
  78.             getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, pageContent).commit();
  79.         }
  80.     }
  81.  
  82.     @Override
  83.     protected void onSaveInstanceState(Bundle outState) {
  84.         super.onSaveInstanceState(outState);
  85.         if (pageContent.isAdded()){
  86.             getSupportFragmentManager().putFragment(outState, KEY_MOVIES, pageContent);
  87.         }
  88.     }
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top