I just googled for "setsupportactionbar fragment", but all I found was "getsupportactionbar from fragment". Is that similar to each other, or is there something special?
Yes it is similar, but you have to keep in mind that you have to place setSupportActionBar() in onCreateView if you want to use elements of fragments layout-file:
..
public class FragmentOne extends Fragment {
..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
Toolbar toolbar = view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
return view;
}
..
}
you should not use setSupportActionBar() in fragment. because every time you add this fragment on activity a new instance of toolbar is created. so every time a new Toolbar is showing in screen. so it is not a good approach.
Related
I am developing an application where i need to call two fragments on an activity replacing another fragment,one of the fragment will contain a Form and another fragment will contain another Form ,i don't have any idea how to do this,so any suggestion will be cordially appreciated,thanks in advance....
I want to replace Form on Click button. by R&D i come to know at this point i need to use to fragment with two layout each layout will contain Forms.
Main Activity.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Fragment 1.
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1, container, false);
}
Fragment 2.
public class Fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment2, container, false);
}
}
You can try like this
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.add(Home.newInstance(),"First");
ft.add(Second.newInstance(),"Second");
ft.commit();
So here you are added multiple fragment into the stack
If you are trying to replace Fragment1 with Fragment2, you replace the fragment.
https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#replace(int,%20android.support.v4.app.Fragment)
I have an HomeAcivity with 4 fragments. 3 of the fragments have no Toolbar of their own and I am showing the Activity's Toolbar in them. However, the 4th Fragment have Toolbar of its own.
Currently I am seeing 2 Toolbars in 4th Fragment, one its own and other that of Activity.How can I hide the Activity's Toolbar and just show its own Toolbar?
Please note that I don't want to update the content of Activity's Toolbar instead of showing new Toolbar inside Fragment. I want to show Fragments own Toolbar and hide that of Activity.
Please help me out.
You can hide activity's toolbar from your 4th fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_view, container, false);
((ToolbarView)getActivity().findViewById(R.id.toolbarView)).setVisibility(View.GONE);
return rootView;
}
and show activity's toolbar from other three fragments
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_view, container, false);
((ToolbarView)getActivity().findViewById(R.id.toolbarView)).setVisibility(View.VISIBLE);
return rootView;
}
i have a activity A with fragment FA, from this fragment i go to FAB, this last fragment is a FragmentPagerAdapter.
When i go from A to FA to FB, press back button and return to FA, and go again to FB this fragment is not showing anything.
My getView method from pager adapter its not called.
This is my transactions code:
From A to FA:
private void setFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment replyGroupsFragment = new ReplyGroupsFragment();
Bundle bundle = new Bundle();
bundle.putString("title", reportName);
replyGroupsFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.container, replyGroupsFragment, "ReplyGroupsFragment").commit();
getSupportActionBar().setTitle(getString(R.string.info));
getSupportActionBar().setDisplayShowTitleEnabled(true);
}
From FA to FB (Use butterkniffe)
#OnItemClick(R.id.listViewReplyGroup)
void listClick(int position) {
List<DomainReplie> domainReplieArrayList = generateRepliesList(position);
setRepliesFragment((ArrayList<DomainReplie>) domainReplieArrayList);
}
And my PagerFragment contains this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ReplyGroupsActivity replyGroupsActivity = (ReplyGroupsActivity) getActivity();
List<DomainReplie> replies = getArguments().getParcelableArrayList("replies");
adapter = new PagerAdapter(replyGroupsActivity.getSupportFragmentManager(), this, replies);
setActionBar();
}
#Override
public void onResume() {
super.onResume();
FirextApplication.getInstance().getBus().register(this);
}
#Override
public void onPause() {
super.onPause();
FirextApplication.getInstance().getBus().unregister(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.work_containter_replies, container, false);
ButterKnife.inject(this, view);
pager.setAdapter(adapter);
return view;
}
Life cyrcle on both case are equals, but in second round screen doesnt show anything.
A little bit late, but maybe it will help others.
I had the same problem. Finally I've solved it and I made the same mistake as you... Here is the line which is causing the problem:
fragmentManager.beginTransaction().replace(R.id.container, replyGroupsFragment, "ReplyGroupsFragment").commit();
You are calling replace(), but replace() just replaces current fragment - you have to call add() for adding another fragment to back stack - that's also what fixes ButterKnife to load/update views again.
Note: ButterKnife changes a lot! Use bind() instead of inject()
I have decided my layout on the basis of JSON response and make a layout at run time without any xml . I just take a Activity and call a api that give me json array on the basis of which i make my layout. I have no problem with that but now i want to attach a navigation drawer with this activity . I have used navigation drawer in my previous Activity by making them fragment but in this case i don't know how to convert my activity to fragment because in navigation drawer we need fragment not activity.
i just do not know how to convert this Dynamic ui to fragment
View rootView = inflater.inflate(R.layout.`dashboard`,container, false);
what should i write in place of dashboard because i have no xml for that particular activity ..
I did it this way:
public class BaseFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ScrollView scrollView = new ScrollView(getActivity());
View view = //view from JSON
scrollView.addView(view);
return scrollView;
}
}
in navigation drawer handler
getFragmentManager().beginTransaction().replace(R.id.content_frame, new BaseFragment()).commit();
I am using FragmentAcitvity in my Tab Bar .
I am able to load a new fragment on tab changed.
But i have a problem Because i have many activties in one tab.
for example i have 2 tabs :
Tab1 , Tab2. and i have in
Tab1 : activityA-->activityB---ActivityC
and
Tab2: activityF
how i can achieve this.
my code is here.
Main Class
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
Context context=this;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(context, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("Records").setIndicator("")), ActiovityA.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("tab2"),acitvityf.class, null);
mTabHost.setCurrentTab(0);
}
}
my acitvityA is here
public class acitvityA extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.acitvitya_xml, container, false);
return v;
}
}
myactivityB is
public class acitvityb extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.acitvityb_xml, container, false);
return v;
}
}
how i can solve this ?what is missing here.
You might have to do a little bit of homework yourself, but i could give you a hint that could help you.
You have the tabs set well. What you could do is, When the transition from Activity1 to 2 takes place, hide the last fragment from the stack, and then show the Activity 2 fragment in tab one (here you will push the fragment into the stack and show it). Maintain a stack of the fragments you are using, and compare an instance of which fragment has been pushed or popped from the stack to bring back the correct fragment.
Its a combination of fragment transactions add, hide, and stack push and pop.
Stack is needed for back press events.
Hope that helps.