I have a problem: backstack does not return title in action bar.
I have this (working) code fragment:
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
rootview = inflater.inflate(R.layout.list_products_layout,container,false);
((MainActivity)getActivity()).restoreActionBar(getString(R.string.title_list_products));
return rootview;
}
But when I want to create Menu in ActionBar, I must call SetHasOptionsMenu(true) in onCreateView (above).
After, menu works but backstack does not change title when I BackPressed
Related
Called from MapFragment
ScheduleFragment scheduleFragment = new ScheduleFragment();
scheduleFragment.showNow(getChildFragmentManager(),scheduleFragment.getTag());
ScheduleFragment
public class ScheduleFragment extends BottomSheetDialogFragment {
public ScheduleFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_schedule, container, false);
return view;
}
}
Now the problem I am facing is whenever the app is resumed the modal bottomsheet appears to be changing state from hidden to expanded is there any fix for this?
I have selected a tabbed activity template with "Action bar tabs" in android studio.
Then i had created three fragments (One.java, Two.java, Three.java) one for each of the tabs.
I have the code related to the cursors in the second fragment.The thing is when i run the application the code in the second fragment is not being executed.
My question is will the fragment (Two.java) will be called implicitly or it is needed to call by creating an instance (if yes, how?).
code snippet for MainActivity.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER)==1){
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
return rootView;
}
else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
View rootView = inflater.inflate(R.layout.fragment_two, container, false);
return rootView;
} else {
View rootView = inflater.inflate(R.layout.fragment_three, container, false);
return rootView;
}
}
code snippet for Two.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
logReader1 = new LogReader(getContext());
db = logReader1.getReadableDatabase();
Cursor logCursor = db.rawQuery("SELECT rowid _id,name,Time_Used,app_Image FROM logs ORDER BY Time_Used DESC", null);
LogCursorAdapter logAdapter = new LogCursorAdapter((MainActivity) getContext(), logCursor);
ListView log_Items = (ListView) container.findViewById(R.id.list_item);
log_Items.setAdapter((logAdapter));
return log_Items;
}
From what I can tell you aren't actually creating a new fragment. You're inflating the layout that you're intending to use as the layout for the fragment two.
Something like https://developer.android.com/training/implementing-navigation/lateral.html should get you pointed in the right direction. Notice how they create and return a fragment in the DemoCollectionPagerAdapter.
how to load Gesturelibrary inside Fragment Activity ...Please help me
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_a, container, false);
//error is here..."this" is not working
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
gestureOverlayView = (GestureOverlayView)view.findViewById(R.id.gestureOverlayView);
return view;
}
It may be asking for context and it's throwing error because fragment doesn't have context.
Try to use like this:
mLibrary = GestureLibraries.fromRawResource(getActivity(), R.raw.gestures);
I've got a problem. For school project I have to create app in Android Studio with few activities so i decided to add Navigation Drawer, but when i try to create simple calculator to app i can't even reference button or textview. This is code for activity where I want to create calculator. Also I have to add Map Activity and SQLite Database.
public class Calculator_main extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.calculator, container, false);
return rootview;
}
}
Use rootView to find your item when referencing in a Fragment:
public class Calculator_main extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.calculator, container, false);
//Notice the TextView being referenced with rootView in front of the findViewById statement
TextView namefield = (TextView) rootView.findViewById(R.id.namefield);
return rootview;
}
}
You need to do this when using Fragment, as this is the only way your app can reference an item in your Fragment's XML view
This is however not necessary in an Activity and should not be confused with the method of referencing in a Fragment
I have two fragments that contains ViewPager. The first fragment that contain viewPager works very well. As I come to second fragment that contain ViewPager, I start to get FragmentStatePagerAdapter NullException. I knew what is the problem, I need to implement onDetach function to get rid this problem. I search around the internet, there is a lots of solutions. And I'm currently using API 19. So maybe this solution very suitable for me. But I don't know how to use the solution.
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2).addToBackStack(null).commit();
What is exactly mFragment1 and mFragment2 and R.id.main ?
Fragment 1
public class fragment1 extends fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_restaurantprocess, container, false);
adapter = new RestaurantProcessPageAdapter(getActivity().getSupportFragmentManager());
...
}
}
Fragment 2
public class fragment2 extends fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_feedpage, container, false);
adapter = new RestaurantFeedPageFragment(getActivity().getSupportFragmentManager());
...
}
}
I'm using slide animation to fragment
public void changeFragment_toRight(Fragment frag) /* Slide to right screen */ {
if (getSupportFragmentManager().findFragmentByTag("frag")!=null)
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.right_slide_in, R.anim.right_slide_out).replace(R.id.container, frag, "frag").commit();
else
getSupportFragmentManager().beginTransaction().add(R.id.container, frag, "frag").commit();
}
changeFragment_toRight(new fragment1());
changeFragment_toRight(new fragment2());
Source: Click here to view the source
It looks like the problem is that you are manually using a FragmentTransaction to change fragments. A ViewPager will do this automatically when you supply it with an adapter, so when you change fragments, you are trying to remove a fragment that has already been removed.