Since I implemented a BottomAppBar my App isn't responding when a certain Fragment should get displayed even though I haven't changed anything. There isn't an error message either and all other Fragments which are getting displayed when another item in the BottomNavigationBar is clicked work fine.
Fragment:
public class StatisticsFragment extends Fragment {
private TabLayout mTabLayout;
private ViewPager mViewPager;
private BankAccountsStatisticsFragment bankAccountsStatisticsFragment = new BankAccountsStatisticsFragment();
private BillsStatisticsFragment billsStatisticsFragment = new BillsStatisticsFragment();
private CategoriesStatisticsFragment categoriesStatisticsFragment = new CategoriesStatisticsFragment();
private GoalsStatisticsFragment goalsStatisticsFragment = new GoalsStatisticsFragment();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_statistics, container, false);
mTabLayout = (TabLayout) inflatedView.findViewById(R.id.tl_statistics);
mViewPager = (ViewPager) inflatedView.findViewById(R.id.vp_statistics);
mViewPager.setAdapter(new Adapter(getChildFragmentManager()));
mTabLayout.setupWithViewPager(mViewPager);
return inflatedView;
}
private class Adapter extends FragmentPagerAdapter {
private static final int TABS = 4;
public Adapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0: return bankAccountsStatisticsFragment;
case 1: return billsStatisticsFragment;
case 2: return categoriesStatisticsFragment;
case 3: return goalsStatisticsFragment;
default: throw new IllegalStateException("Couldn't find a fragment for position " + position);
}
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0: return getString(R.string.tab_bank_accounts);
case 1: return getString(R.string.tab_bills);
case 2: return getString(R.string.tab_categories);
default: return getString(R.string.tab_goals);
}
}
#Override
public int getCount() {
return TABS;
}
}
}
LOGCAT
LOGCAT
Inflating a Layout and creating a Fragment transition (like the "FragmentManager....replace().commit()") are done in the MainThread where the UserInterface is rendered, so the UserInterface will freeze in these moments.
I think you have to pre-load the "bad" Fragment and just HIDE it instead of Destroy it...in this way the next time you want to create it is already ready.
Related
I have an activity and its child Fragment with a LinearLayout that I generate buttons inside of. When the fragment is created, everything runs fine. However, when the parent activity downloads a new item, I call the method in the fragment that is used to generate the buttons and add them to the view, but the LinearLayout returns null and I can't figure out why. I either need to fix it or find a way to "re-display" my fragment. Here is the related code:
SongFragment:
LinearLayout linearLayout;
DatabaseHelper databaseHelper;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_song, container, false);
linearLayout = rootView.findViewById(R.id.songFragmentMainLayout);
databaseHelper = new DatabaseHelper(getActivity());
return rootView;
}
#Override
public void onResume() {
super.onResume();
RefreshButtons();
}
public void RefreshButtons(){
linearLayout.removeAllViews(); //this line is where the NullPointerException is called
...
}
MainActivity:
//refresh fragment view
SongFragment fragment = (SongFragment) sectionsPagerAdapter.getItem(0);
if(downloadQueue.size() == 0){
fragment.RefreshButtons();
Toast.makeText(context, "New songs downloaded", Toast.LENGTH_SHORT).show();
}
...
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new SongFragment();
break;
case 1:
fragment = new PlaceholderFragment();
break;
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Songs";
case 1:
return "Playlists";
}
return null;
}
}
Thanks for your help.
The method fragment.RefreshButtons(); returns an NPE because if you implemented SectionsPagerAdapter like you should, getItem() returns a new Instance of that fragment which is not yet attached to the fragment manager, therefore causing a Nullpointer exception.
So what you should do is get a currently active fragment instance like this:
Fragment frag = (Fragment) mViewPager.getAdapter().instantiateItem(mViewPager, 0);
0 is the position of your fragment, so for example if you have 3 fragments, 0 will return the first fragment instance etc...
I am using two fragments inside viewpager. In the second fragment, I play a video from url using exoplayer. My problem is that when the activity is launch, the video is loaded automatically because all fragments are automatically created by viewpager. How to create one frament at a time so that the second fragment can play the video only if it is visible to the user (by swipping or tapping the tab).
The viewpager adapter
class ViewPagerAdapter extends FragmentStatePagerAdapter {
Bundle bundle;
public ViewPagerAdapter(FragmentManager manager, Bundle bundle) {
super(manager);
this.bundle = bundle;
}
#Override
public Fragment getItem(int position) {
Log.d("getItem", position + "");
switch (position) {
case 0:
IngredientListFragment ingredientListFragment = new IngredientListFragment();
ingredientListFragment.setArguments(bundle);
return ingredientListFragment;
case 1:
StepsListFragment stepsListFragment = new StepsListFragment();
stepsListFragment.setArguments(bundle);
return stepsListFragment;
default:
IngredientListFragment ing = new IngredientListFragment();
ing.setArguments(bundle);
return ing;
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Ingredients";
case 1:
return "Steps";
default:
return "Ingredients";
}
}
}
Inside the activity
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), bundle);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
Please Help.
you do not need to create fragment one at a time:
use this code in second fragment to run exoplayer when user open that fragment:
private boolean _hasLoadedOnce = false; // your boolean field
#Override
public void setUserVisibleHint(boolean isFragmentVisible_) {
super.setUserVisibleHint(true);
if (this.isVisible()) {
if (isFragmentVisible_ && !_hasLoadedOnce) {
// move exoplayer code here so this code run only when user select this fragment
_hasLoadedOnce = true;
}
}
}
One more thing, you have to put this line after creating viewpager:
in your case after
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(1);
This question already has answers here:
ViewPager set current page programmatically
(3 answers)
Closed 4 years ago.
I have 3 tabs containing difference Fragments as:
Product and Services
Business Directory
Job
Bellow is my TabFragement Adapter The code is worked correctly but only 1 thing I want is:
=> I want to set Job Fragment as default in onCreate
This is my adapter.
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new Product_Service();
case 1 : return new Business_Dir();
case 2 : return new Job();
}
return null;
}
#Override
public int getCount() {
return int_items;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Product & Srevices";
case 1 :
return "Business Directory";
case 2 :
return "Job";
}
return null;
}
}
}
You can use the fragment position index to show particular fragment as default using setCurrentItem method ofViewPager docs. Do this after setting adapter on viewpager
viewPager.setCurrentItem(2, false); // job fragment index is 2
false here mean , the transition to fragment 3 will be done instantly and if you pass true then chosen fragment will be displayed using scrolling effects ( you can use true when you want to give a little tour to user for first time)
I have been trying to show different layouts in different tabs in the swipeable TabLayout using PagerTabStrip. Can anybody help?
I want to show one layout in first tab, second different layout in 2nd tab etc.
public class MainActivity extends FragmentActivity {
// create object of FragmentPagerAdapter
SectionsPagerAdapter mSectionsPagerAdapter;
// viewpager to display pages
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the five
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* A FragmentPagerAdapter that returns a fragment corresponding to one of
* the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#SuppressLint("NewApi")
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: {
//Show 1st Layout(Here I need HELP)
//HELP HELP HELP
}case 1:
{
//Show 2nd Layout(Here I need HELP)
//HELP HELP HELP
}
default:
}
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 5 total pages.
return 6;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Section 1";
case 1:
return "Section 2";
case 2:
return "Section 3";
case 3:
return "Section 4";
case 4:
return "Section 5";
case 5:
return "Section 6";
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setTextSize(25);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return textView;
}
}
}
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1: {
rootView = inflater.inflate(R.layout.fragment_bba, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_bcom, container, false);
break;
}
case 3: {
rootView = inflater.inflate(R.layout.fragment_bca, container, false);
break;
}
}
return rootView;
Ok for the people who want to solve this problem using design patterns.
Find full working solution Here.
If u write the fragment based on if-else condition it may solve the problem
switch(fragmentId)
{
case 1:
{
fragment 1 related stuff
}
case 2:
{
fragment 2 related stuff
}
.......
.......
and so on
But the problem with this approach is if in future,
1) you decide to add more fragments
or
2) you decide to change some functionality of existing fragment
Then you will have to modify the existing code (inside if-else condition)
Not a preferred programming practice
Instead you can follow this approach
public abstract class BasicFragment extends Fragment {
public BasicFragment newInstance()
{
Log.d("Rohit", "new Instance");
Bundle args = new Bundle();
// args.putInt(ARG_PAGE, page);
BasicFragment fragment = provideYourFragment();
fragment.setArguments(args);
return fragment;
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater,ViewGroup parent, Bundle savedInstanseState)
{
View view = provideYourFragmentView(inflater,parent,savedInstanseState);
return view;
}
public abstract BasicFragment provideYourFragment();
public abstract View provideYourFragmentView(LayoutInflater inflater,ViewGroup parent, Bundle savedInstanceState);
}
Your Fragment implementation
public class ImageFragment extends BasicFragment{
#Override
public BasicFragment provideYourFragment() {
return new ImageFragment();
}
#Override
public View provideYourFragmentView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.image_fragment,parent,false);
//Get your parent layout of fragment
RelativeLayout layout = (RelativeLayout)view;
//Now specific components here
ImageView imageView = (ImageView)layout.findViewById(R.id.myImage);
imageView.setImageResource(android.R.drawable.ic_media_play);
return view;
}
}
Happy coding
Trying to create application with view pager inside navigation drawer. I've options to select from navigation and depending on that (outer selection), i have to populate different number of fragments in view pager.
I've used tutorials to create navigation drawer and view pager separately from Google docs. As per docs, view pager have fixed number of fragments. if i hard code that way everything works fine.
But, when i try to make it dynamic as per left selection, i'm getting illegal state exception
for instance, I've implemented fragmentstatepageradapter,
public class innerFragment extends Fragment {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public static final String active_outer_option = "section_number";
public innerFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_outer, container,
false);
Log.d("logs", "onCreateView:innerFragment()");
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(Integer
.parseInt(getArguments().getString(active_outer_option)));
Log.d("logs", "onAttach:innerFragment()");
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private int active_outer_option= 0;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Log.d("logs", "getItem(int " + position + ")");
active_outer_option = position;
Bundle b = new Bundle();
b.putString("section_number", "" + position);
DummySectionFragment fragment = new DummySectionFragment();
fragment.setArguments(b);
return fragment;
}
#Override
public int getCount() {
switch (active_outer_option) {
case 0:
return 9;
case 1:
return 2;
case 2:
return 2;
case 3:
return 2;
case 4:
return 4;
default:
return 0;
}
}
#Override
public CharSequence getPageTitle(int position) {
Log.d("logs", "getPageTitle(int " + position + ")");
return "OP " + String.valueOf(position);
}
}
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("logs", "onCreateView:DummySectionFragment()");
View rootView = inflater.inflate(R.layout.fragment_inner,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(getArguments().getString(ARG_SECTION_NUMBER));
return rootView;
}
}
}
when i run this application, its giving "Unfortunately application stopped working" and exception is:
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 9, found: 2 Pager id: com.xxx.xxx:id/pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.xxx.xxx.innerFragment$SectionsPagerAdapter
any idea whats wrong with that exception and code?
Edit:
from my debugging first everything is fine and gettitle is called twice with index 0 and 1 and after that getcount is called with index and getting exception.
does this ring any bells?