I am trying to implement tablayout in a fragment which is already part of BottomNavigationView, I am facing a problem while navigating through navigation bar, There is a recycler view in a fragment which is the part of tablayout, now when I navigate between fragments via BottomNavigation the recyclerview becomes empty, and there is nothing in the logcat.
MainActivity
public class MainActivity extends AppCompatActivity {
private BottomNavigationView navigation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
initComponent();
loadFragment(new HomeFragment());
}
private void initComponent() {
navigation = findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// ViewAnimation.fadeOutIn(fragme);
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.home_main:
fragment = new HomeFragment();
break;
case R.id.search:
fragment = new SearchFragment();
break;
case R.id.notifications:
fragment = new NotificationsFragment();
break;
case R.id.favourite:
fragment = new FavouriteFragment();
break;
}
return loadFragment(fragment);
}
});
findViewById(R.id.bt_menu).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
HomeFragment
public class HomeFragment extends Fragment {
private ViewPager view_pager;
private TabLayout tab_layout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, null);
view_pager = view.findViewById(R.id.view_pager);
setupViewPager(view_pager);
tab_layout = view.findViewById(R.id.tab_layout);
tab_layout.setupWithViewPager(view_pager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());
adapter.addFragment(new PresetsFragment(), "Presets");
/* adapter.addFragment(new PresetsFragment(), "Tutorials");
adapter.addFragment(new PresetsFragment(), "Categories");*/
viewPager.setAdapter(adapter);
}
#Override
public void onDestroy() {
super.onDestroy();
tab_layout = null;
view_pager = null;
}
PresetsFragment
public class PresetsFragment extends Fragment {
RecyclerView recyclerView;
PresetsAdapter presetsAdapter;
String[] presetList = {"s", "s", "d", "d"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_presets, container, false);
recyclerView = view.findViewById(R.id.presetsRecyclerView);
presetsAdapter = new PresetsAdapter(presetList, getActivity());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(presetsAdapter);
//presetsAdapter.notifyDataSetChanged();
return view;
}
#Override
public void onResume() {
super.onResume();
}
}
I have spent so many hours to figure it out but still unable to get it..
from your loadFragment method you are using FrameLayout and replacing fragment on BottomNavigation selection when you do so you are creating a new fragment every time if you used a ViewPager it will work fine with you
Related
I am designing an app that has 4 different fragments on the homeActivity using bottomNavigation to switch between them and on the first page that I am using as a news feed, the items do not show until I switch to a different fragment, then back again. I understand there are problems similar to this on SO but no solutions seem to help in my case.
I assume the problem is something to do with the difference in onCreate() and onCreateView in fragments.
Any help is appreciated.
public class HomeFragment extends Fragment {
View v;
private RecyclerView recyclerView;
private List<Drink> drinksList;
RecyclerViewAdapter recyclerViewAdapter;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_home, container, false);
return v;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drinksList = new ArrayList<>();
drinksList.add(new Drink("Latte",999,"Coffee","test",1.99,77,R.drawable.latte));
drinksList.add(new Drink("Black Tea",123,"Tea", "test", 1.50,11,R.drawable.blacktea));
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = v.findViewById(R.id.recyclerView);
recyclerViewAdapter = new RecyclerViewAdapter(drinksList,getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.notifyDataSetChanged();
}
}
EDIT
adding HomeActivity
public void setUpMenuNav() {
mMainFrame = findViewById(R.id.mainFrame);
bottomNavigationView = findViewById(R.id.bottomNaviationView);
homeFragment = new HomeFragment();
socialFragment = new SocialFragment();
trackingFragment = new TrackingFragment();
locationFragment = new LocationFragment();
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navHome:
setFragment(homeFragment);
return true;
case R.id.navSocial:
setFragment(socialFragment);
return true;
case R.id.navTracking:
setFragment(trackingFragment);
return true;
case R.id.navLocation:
setFragment(locationFragment);
return true;
default:
return false;
}
}
});
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.mainFrame, fragment);
fragmentTransaction.commit();
}
Then setUpMenuBar is called in the onCreate() method
on your onCreate() method on HomeActivity add setFragment(new HomeFragment()).
that should do the trick.
I am trying to replace the current active fragment inside ViewPager when item inside fragment's recycelerview is clicked. So far, I manage to pop up the Toast on the second fragment when the item on the first fragment is clicked, but the all the view of the second fragment is blank.
Here is my code:
MainActivity.class
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = findViewById(R.id.view_pager);
FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(adapter.getCount() - 1);
}
// ..........
private static class FragmentAdapter extends FragmentStatePagerAdapter {
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return AllGenreFragment.newInstance();
case 1:
return DashboardFragment.newInstance();
case 2:
return NotificationFragment.newInstance();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
// ..........
}
AllGenreFragment.class
public class AllGenreFragment extends Fragment {
RecyclerView genreRV;
private AllGenreAdapter allGenreAdapter;
private List<Genre> genreList;
public static AllGenreFragment newInstance() {
return new AllGenreFragment();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_genre, container, false);
genreRV = v.findViewById(R.id.rv);
genreRV.setHasFixedSize(true);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
genreRV.setLayoutManager(layoutManager);
genreList = new ArrayList<>();
allGenreAdapter = new AllGenreAdapter(genreList, getContext());
genreRV.setAdapter(allGenreAdapter);
return v;
}
}
AllGenreAdapter.class
public class AllGenreAdapter extends RecyclerView.Adapter<AllGenreAdapter.ViewHolder> {
// .......
class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder {
public ViewHolder(final View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Genre: " + genre_title.getText().toString(), Toast.LENGTH_SHORT).show();
AppCompatActivity activity = (AppCompatActivity) itemView.getContext();
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment allAnimeFragment = new AllAnimeFragment();
Bundle bundle = new Bundle();
bundle.putString("genreid", genre_id.getText().toString().trim());
allAnimeFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.view_pager, allAnimeFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
// .......
}
AllAnimeFragment.class
public class AllAnimeFragment extends Fragment {
RecyclerView animeRV;
private AllAnimeAdapter allAnimeAdapter;
private List<Anime> animeList;
String genreid;
public static AllAnimeFragment newInstance() {
return new AllAnimeFragment();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_anime, container, false);
Bundle arguments = getArguments();
genreid = arguments.getString("genreid");
Toast.makeText(getContext(), genreid, Toast.LENGTH_SHORT).show();
// .....
return v;
}
}
Your approach is not really good, try below way:
Create a container fragment, called ContainerFragment, example, you will be using this fragment for your ViewPager.
The ContainerFragment should contain a container view, and add AllGenreFragment in the first them it is invoked.
In AllGenreFragment, create a callback, when an item in the RecyclerView be clicked, then in ContainerFragment will replace (or add) current fragment (AllGenreFragment) to AllAnimeFragment
First, create ContainerFragment,
public class ContainerFragment extends Fragment implements AllGenreFragment.OnAllGenreFragmentListener {
#Nullable #Override public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_container, container, false);
}
#Override public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initialLoad();
}
private void initialLoad() {
getChildFragmentManager()
.beginTransaction()
.add(R.id.container, AllGenreFragment.newInstance())
.commit();
}
#Override public void onItemClicked() {
getChildFragmentManager()
.beginTransaction()
.replace(R.id.container, AllAnimeFragment.newInstance())
.commit();
}
}
This Fragment should implement a callback from AllGenreFragment for handling fragment transaction.
also, the xml layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Next step, add above fragment into ViewPager, we using ContainerFragment instead of AllGenreFragment.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return ContainerFragment().newInstance();
case 1:
return DashboardFragment.newInstance();
case 2:
return NotificationFragment.newInstance();
}
return null;
}
Ok, so now come back to AllGenreFragment, we should create a CallBack that we implement in ContainerFragment
public interface AllGenreFragmentListener {
onItemClicked();
}
and then,
private AllGenreFragmentListener listener;
#Override public void onAttach(Context context) {
super.onAttach(context);
if (getParentFragment() instanceof AllGenreFragmentListener) {
listener = (AllGenreFragmentListener) getParentFragment();
}
}
so we have an instance of AllGenreFragmentListener, from now, whenever listener call onItemClicked, the method onItemClicked in ContainerFragment will be invoked.
Ok, let do it,
create below variable and method in your AllGenreAdapter:
private AllGenreFragmentListener listener;
public void setListener(AllGenreFragmentListener listener) {
this.listener = listener;
}
and update your ViewHodler
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Genre: " + genre_title.getText().toString(), Toast.LENGTH_SHORT).show();
if (listener != null)
listener.onItemClicked()
});
}
Don't forget call setListener method in your AllGenreFragment, so please add
#Override public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (allGenreAdapter != null) allGenreAdapter.setListener(listener);
}
That all,
I type this code by hand, not in my IDE so maybe it not compile, so please fix it if you face any compile error.
Note
If you want to keep the behavior of the BACK button, override onBackPressed in your MainActivity
#Override public void onBackPressed() {
Fragment f = getCurrentFragment();
if (f != null) {
if (f.getChildFragmentManager().getBackStackEntryCount() > 1) {
f.getChildFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}
public Fragment getCurrentFragment() {
return (Fragment) viewPager.getAdapter()
.instantiateItem(viewPager, viewPager.getCurrentItem());
}
Also, add fragment into backStack whenever we add them to the container view (in your ContainerFragment)
private void initialLoad() {
getChildFragmentManager()
.beginTransaction()
.add(R.id.container, AllGenreFragment.newInstance())
.addToBackStack(null)
.commit();
}
#Override public void onItemClicked() {
// change `replace` to `add` and add more `.addToBackStack(null)`
getChildFragmentManager()
.beginTransaction()
.add(R.id.container, AllAnimeFragment.newInstance())
.addToBackStack(null)
.commit();
}
you should add a context in the constructor of your adapter
public class AllGenreAdapter(Context context) extends RecyclerView.Adapter<AllGenreAdapter.ViewHolder> {
// .......
}
then pass your activity to your adapter, use it to replace your fragment
I have Navigation drawer.and in First fragmant of Navigation drawer there is viewpager and there is information coming from network.Viewpager has 2 fragments also.When I switch fragment to Settings fragment of navigation drawer and coming back to this fragment everything is gone..There is no any information..How can i save state of this fragment?Or in which method can i send request to take information from network again
MainActivity
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.motherFragments, FragmentMainPage.newInstance("MainPage"))
.commit();
}
#Override
public void onBackPressed() {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
drawer.openDrawer(GravityCompat.START);
break;
}
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
if (item.isChecked()) {
drawer.closeDrawer(GravityCompat.START);
return false;
}
switch (item.getItemId()) {
case R.id.home: {
fragment = new FragmentMainPage().newInstance("Sample");
break;
}
case R.id.settings:{
Intent intent = new Intent(this,SettingsActivity.class);
startActivity(intent);
break;
}
case R.id.incoming:{
fragment = new FragmentIncomingPage().newInstance("Sample");
break;
}
}
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction().replace(R.id.motherFragments, fragment)
.commit();
item.setChecked(true);
}
drawer.closeDrawer(GravityCompat.START);
return true;
} }
Fragment1 of Navigation drawer
public class FragmentMainPage extends Fragment implements
ViewPager.OnPageChangeListener {
private Toolbar toolbar;
private MainActivity activity;
private TabLayout tabLayout;
private ViewPager viewPager;
String[] names = {
"Sample 1",
"Sample 2"
};
private static FragmentMainPage myInstance;
public static FragmentMainPage newInstance(String testString){
Bundle bundle = new Bundle();
bundle.putString("test",testString);
if(myInstance == null){
myInstance = new FragmentMainPage();
}
myInstance.setArguments(bundle);
return myInstance;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
activity = ((MainActivity) context);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fragment_main,container,false);
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
activity.setSupportActionBar(toolbar);
activity.setTitle(getResources().getText(R.string.bosh_sahifa));
activity.getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewpager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(this);
setupTabName();
return view;
}
private void setupViewpager(ViewPager viewPager) {
MyAdapter adapter = new MyAdapter(getActivity().getSupportFragmentManager());
adapter.addFragement(new Fragment_home1());
adapter.addFragement(new Fragment_home2());
viewPager.setAdapter(adapter);
}
private void setupTabName() {
tabLayout.getTabAt(0).setText(names[0]);
tabLayout.getTabAt(1).setText(names[1]);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
private class MyAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragement(Fragment fragment){
mFragmentList.add(fragment);
}
}
}
Fragment 2 of Navigation drawer
public class FragmentIncomingPage extends Fragment {
private Toolbar toolbar;
private MainActivity activity;
private static FragmentIncomingPage myInstance;
public static FragmentIncomingPage newInstance(String testString){
Bundle bundle = new Bundle();
bundle.putString("test",testString);
if(myInstance == null){
myInstance = new FragmentIncomingPage();
}
myInstance.setArguments(bundle);
return myInstance;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
activity = ((MainActivity) context);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fragment_incoming,container,false);
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
activity.setSupportActionBar(toolbar);
activity.setTitle(getResources().getText(R.string.Incoming));
activity.getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return view;
}
}
Have 3 fragments and swipe to 3rd fragment then back to 1st fragment, the 1st fragment's data is gone?
You can use fragment.addToBackStack(null) method.
like,
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, A);
ft.addToBackStack(null);
ft.commit();
There is few ways to achieve it.
You can use SharedPreferences to save current Fragment by mViewPager.getCurrentItem() and set it again once you come to the ViewPager activity by navigation drawer button.
Use BaseFragment and implement your fragments from the Base Fragment, keep current visible fragment of the view pager in side the Base Fragment as protected variable.
As Base Fragment which is describe above, You can use Bas Activity class.
I've a FrameLayout and Navigation Drawer in Main Activity. I'm transacting a fragment in Main Activity using this FrameLayout. In this fragment, I've TabLayout and ViewPager. Now what I want is to open a new fragment on Button click in the ViewPager fragment. But app crashes on Button click. Is it becuz FrameLayout is in Main Activity and the Button is in child fragment's child fragment? I've tried using FrameLayout in Tabs but that would mean I would have to use FrameLayouts for each Tab and I don't want that. So any help in this regard will be much appreciated. Also, how to handle back arrow click in toolbar to go back to previous fragment?
public class HomeFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.homefragment, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
#Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new TabOne();
case 1 : return new TabTwo();
}
return null;
}
#Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "ONE";
case 1 :
return "TWO";
}
return null;
}
}
}
public class TabOne extends Fragment {
private Button newFrag;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tabone, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
newFrag = (Button)view.findViewById(R.id.button);
newFrag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction xfragmentTransaction = getChildFragmentManager().beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new NewFrag()).addToBackStack(null).commit();
}
});
}
}
public class MainActivity extends AppCompatActivity {
DrawerLayout myDrawerLayout;
NavigationView myNavigationView;
Toolbar toolbar;
ActionBarDrawerToggle mDrawerToggle;
FragmentManager myFragmentManager;
FragmentTransaction myFragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
myNavigationView = (NavigationView) findViewById(R.id.nav_drawer) ;
myFragmentManager = getSupportFragmentManager();
myFragmentTransaction = myFragmentManager.beginTransaction();
myFragmentTransaction.replace(R.id.containerView, new HomeFragment()).commit();
myNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem selectedMenuItem) {
myDrawerLayout.closeDrawers();
if (selectedMenuItem.getItemId() == R.id.nav_profile) {
FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new ProfileFragment()).addToBackStack(null).commit();
}
if (selectedMenuItem.getItemId() == R.id.nav_history) {
FragmentTransaction xfragmentTransaction = myFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new HistoryFragment()).addToBackStack(null).commit();
}
return false;
}
});
toolbar = (Toolbar)findViewById(R.id.toolbar);
mDrawerToggle = new ActionBarDrawerToggle(this, myDrawerLayout, toolbar, R.string.app_name, R.string.app_name);
myDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
FATAL EXCEPTION: main
Process: com.sam.demonavigation, PID: 8523
java.lang.IllegalArgumentException: No view found for id 0x7f0c006b (com.sam.demonavigation:id/containerView) for fragment HistoryFragment{42016928 #0 id=0x7f0c006b}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5388)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
at dalvik.system.NativeStart.main(Native Method)
This link proved to be very useful. Using the method mentioned in my link I was able to use the FrameLayout in my Main Activity to open any fragment
// This is the button click event in one of the TabLayout fragment
newFrag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment var1 = new NewFrag();
((MainActivity)getActivity()).replaceFragment(var1);
}
});
// Add this method in MainActivity
public void replaceFragment(Fragment fragment){
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.containerView, fragment).addToBackStack(null);
t.commit();
}
.
1-Create ViewPager object
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
setupViewPager(viewPager);
2-then add Custom FragmentPagerAdapter
ViewPagerAdapter adapter = new ViewPagerAdapter(this);
adapter.addFrag(new Fragment1(), "Item1");
adapter.addFrag(new Fragment2(), "Item2");
adapter.addFrag(new Fragment3(), "Item3");
viewPager.setAdapter(adapter);
3-After that write custom adapter definition
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
As mentioned in my previous question I had some problem with showing Fragments inside a ViewPager after opening another Fragment from one of the Fragments inside the ViewPager.
I somehow managed to resolve the problem by using add() in the FragmentTransaction. Unfortunately this creates another problem:
I am using the ActionBar in my app. When I am selecting the same menu item twice I encounter the same problem as before, it doesn't show any Fragments inside the ViewPager.
Home Activity Code(where action bar on click events are call)
public class HomePageActivity extends SherlockFragmentActivity implements ICallback {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private SherlockActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mChampionsMenuItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_Sherlock);
setContentView(R.layout.activity_home_page);
mTitle = mDrawerTitle = getTitle();
mChampionsMenuItems = getResources().getStringArray(R.array.champions_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mChampionsMenuItems));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.navy_blue));
getSupportActionBar().setDisplayShowTitleEnabled(false);
mDrawerToggle = new SherlockActionBarDrawerToggle(this, mDrawerLayout, R.drawable.menu_icon, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
onFragmentChange(new HomeActivityFragment(), true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
default: return super.onOptionsItemSelected(item);
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
switch (position) {
case 0:
onFragmentChange(new Fragment1(), false);
break;
case 1:
onFragmentChange(new Fragment2(), false);
break;
case 2:
onFragmentChange(new MyPointFragment3(), false);
break;
case 3:
// onFragmentChange(new HomeActivityFragment(), true);
break;
case 4:
onFragmentChange(new QueriesFragment(), false);
break;
case 5:
onFragmentChange(new GalleryFragment(), false);
break;
default:
break;
}
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onFragmentChange(Fragment fragment, boolean flag) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.content_frame, fragment, fragment.getClass().getName());
transaction.commit();
}
}
This is the Fragment which contains the ViewPager:
public class Fragment1 extends Fragment implements OnClickListener {
ICallback callback;
private LinearLayout headerContainer;
private ImageView headerLogo;
private TextView headerName;
private Button menuBarButton;
MyAdapter adapter;
ViewPager pager;
ActionBar actionBar;
private Button progOverview, progStr, bonusPoint;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof ICallback) {
this.callback = (ICallback) activity;
}
actionBar = activity.getActionBar();
actionBar.show();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = setUpView(inflater, container);
return rootView;
}
private View setUpView(LayoutInflater inflater, ViewGroup container) {
View rootView = inflater.inflate(R.layout.fragment_about_champions_club, container, false);
headerContainer = (LinearLayout) rootView.findViewById(R.id.second_top_header);
headerContainer.setBackgroundColor(getResources().getColor(R.color.prog_str_blue));
headerLogo = (ImageView) rootView.findViewById(R.id.header_logo);
headerLogo.setBackgroundResource(R.drawable.about_champions_ticon);
headerName = (TextView) rootView.findViewById(R.id.header_name);
headerName.setText(R.string.about_champ_title);
adapter = new MyAdapter(getFragmentManager());
pager = (ViewPager) rootView.findViewById(R.id.pager);
pager.setAdapter(adapter);
return rootView;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
// Restore last state for checked position.
}
}
}
}
This is one of the Fragments inside the ViewPager:
public class ProgramOverViewFragment extends Fragment {
ICallback callback;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof ICallback) {
this.callback = (ICallback) activity;
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_program_overview, container, false);
TextView termsAndCond = (TextView) rootView.findViewById(R.id.terms_and_condition_button);
termsAndCond.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment);
fragment.setRetainInstance(true);
transaction.addToBackStack(null);
transaction.commit();
}
});
return rootView;
}
}
My FragmentStatePagerAdapter:
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int index) {
switch (index) {
case 0: return new ProgramOverViewFragment();
case 1: return new ProgramStructureFragment();
case 2: return new BonusPointFragment();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
Thanks for your help!
What do you want to do exactly? Are you trying to replace a Fragment inside the ViewPager? Or are you trying to replace a Fragment inside a Fragment inside the ViewPager? Or are you trying to do something different altogether? I can only give you an accurate solution if you explain what you are trying to do in detail. Anyway here is what I am guessing might be wrong:
I think your problem is that you use the wrong FragmentManager. There are multiple different FragmentManagers, but essentially there are two cases:
You want to add/replace/remove a Fragment from an Activity
You want to add/replace/remove a Fragment from another Fragment
As long as I don't see your layout files I can't be sure which of those two options fits your situation.
Solution if you want to replace a Fragment inside of another Fragment
If you want to perform a FragmentTransaction on a Fragment inside of another Fragment, then you need to use the child FragmentManager! You can use the child FragmentManager inside a Fragment like this:
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment);
fragment.setRetainInstance(true);
transaction.addToBackStack(null);
transaction.commit();
As you can see you just need to use getChildFragmentManager() instead of getSupportFragmentManager()
As I already said, without any further information about your situation I cannot give you a more accurate answer. So please try to explain what you want to do as best as you can to me and I'm confident we can solve this.
Finally i found what i did wrong in my code :
i'm using this below code for setting adapter for view Pager inside a fragment :
adapter = new MyAdapter(getFragmentManager());
pager = (ViewPager) rootView.findViewById(R.id.pager);
pager.setAdapter(adapter);
and by replacing getFragmentManager() to getChildFragmentManager() we resolve this problem. As per my understanding i'm using view pager inside a fragment that's why i have to use getChildFragment manager to store transitions of my viewPager.
Might this help someone and this question is still open for better solutions.