Is it possible to get the attach complete of a fragment in the hosting activity? I am new to Android.
Here is my code to add a fragment:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
DecorationFragment fragment = new DecorationFragment();
ft.add(R.id.fragmentContainer,fragment,"decoration_fragment");
ft.commit();
I have 2 objects in the Fragment. I want to handle their click event in the Activity.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_decoration, container, false);
mChangeTextColorButton = (Button) rootView.findViewById(R.id.changeTextColorButton);
mColorPallette = (LinearLayout)rootView.findViewById(R.id.color_pallette_linearView);
return rootView;
}
How to achieve this?
Not sure exactly what you're asking, but perhaps this is what you're looking for:
https://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#onAttachFragment(android.support.v4.app.Fragment)
void onAttachFragment (Fragment fragment)
Called when a fragment is attached to the activity.
This is called after the attached fragment's onAttach and before the attached fragment's onCreate if the fragment has not yet had a previous call to onCreate.
Related
i am trying to replace a fragment listview with a new fragment when i am use onclicklistener. However, when i click in one of the listview items change fragment but the listview appers below. Here my code:
changing listview fragment
ent_qtb_operaciones user = listaqtb_operaiones.get(pos);
qtb_operaciones_deta fragment = new qtb_operaciones_deta();
Bundle bundle = new Bundle();
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
bundle.putSerializable("usuario",user); // use as per your need
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.qtb_listview_layout,fragment);
fragmentTransaction.commit();
the fragment which receive inforamaition
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vista = inflater.inflate(R.layout.qtb_operacion_base, container, false);
return vista;
}
As result, Second fragment superpose first one but listview of first fragment does not desappair
I use one activity multiple fragments in my app. I use a shared element animation. I have two fragments, one of them is a detail page.
I don't want to addToBackStack function for fragment transition. And the detail fragment returns without animation for some cases. (fragmentMain->fragmentDetail)
DetailFragment detailFragment = new DetailFragment();
Transition moveTransition = TransitionInflater.from(MainActivity.singleInstance)
.inflateTransition(android.R.transition.move); // android.R.transition.move
moveTransition.setDuration(400);
detailFragment.setSharedElementEnterTransition(moveTransition);
FragmentManager fragmentManager = MainActivity.getActivityFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
.addSharedElement(sharedView, sharedView.getTransitionName())
.replace(R.id.mainLayout, detailFragment);
fragmentTransaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
This code works for me. I know that if I use addToBackStack, fragmentDetail->fragmentMain reveal animation works automatically. But I don't want to the use back stack.
Below code doesn't works for fragmentDetail->fragmentMain.
detailFragment.setSharedElementReturnTransition(moveTransition);
mainFragment.setSharedElementEnterTransition(moveTransition);
FragmentManager fragmentManager = MainActivity.getActivityFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
.addSharedElement(textView, textView.getTransitionName())
.replace(R.id.mainLayout, mainFragment);
fragmentTransaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
How can I do shared element animation for this case?
Look at your onCreateView function. If It recreate the layout, it breaks the transition id.
Solution:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(inflatedView != null)
return inflatedView;
inflatedView = inflater.inflate(R.layout.fragment_blank, container,
return inflatedView;
}
I have an Activity with a Layout named "container" where Fragments are inflated in using FragmentTransactions
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
transaction.replace(R.id.container, SomeFragment.newInstance());
transaction.addToBackStack(null);
transaction.commit();
One Fragment inflates another fragment with two more Fragments in a TabLayout. This is done with a TabLayout and a ViewPager. Everything works well until I pop it from the backstack.
getSupportFragmentManager().popBackStack();
When I do this, the tabs are recreated and all the lifecycle methods are called. However, they have no content. They layouts seem not to be inflated.
I have a method called initUI(View v) which is called in
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabs, container, false);
initUI(rootView);
return rootView;
}
It does the setup for the TabLayout.
private void initUI(View rootView) {
pager = (ViewPager) rootView.findViewById(R.id.viewPagerCollected);
tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
pager.setAdapter(new MyPagerAdapter(getActivity().getSupportFragmentManager()));
tabLayout.setupWithViewPager(pager);
tabLayout.setVisibility(View.VISIBLE);
}
Again, this method is called but doesn't seem to do anything when popping the backstack.
What am I doing wrong here? Why is the Fragment not recreated properly even though the onCreateView and initUI are being called?
EDIT: In my TabsFragment, each Fragment has a RecyclerView. When clicking one of the Items, another Fragment opens up, displaying detailed information to the user.
Here the user can navigate back. When doing this, the TabsFragment is visible again but this time without any content. It seems like the inner fragments have not being inflated this time around.
First Add to back stack like this...
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
transaction.replace(R.id.container, SomeFragment.newInstance());
transaction.addToBackStack("YOUR_TAG");
transaction.commit();
After than try to popstackback()
I am working on App where i am attaching 5 fragments on an activity. Everything is working great but when i put my app in background from any fragment and after some time when my app resumes it crashes. I get reference of my Activty as null. here is my code
This is code in Activty from where i am attaching fragment
FragmentManager fragmentManager = getSupportFragmentManager();
searchFragment = SearchFragment.newInstance(MainBaseActivity.this);
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frameLayoutMain, searchFragment, "SearchFragment");
fragmentTransaction.commit();
And this is my Fragment class
public static SearchFragment newInstance(MainBaseActivity mainBaseActivity) {
fragment = new SearchFragment();
fragment.mainBaseActivity = mainBaseActivity;
fragment.inflater = (LayoutInflater) mainBaseActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
fragment.myApplication = ((MyApplication) mainBaseActivity.getApplicationContext());
return 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.search_fragment, container, false);
preferences = mainBaseActivity.getSharedPreferences(Constant.CHAI_APP, Context.MODE_PRIVATE); // here i get null pointer
editor = preferences.edit();
return view;
}
Fragments can be killed and recreated by the system at various times. You cannot trust the kind of initialization you do in your newInstance() - when the fragment is recreated, the fields won't be initialized.
Remove these initializations:
fragment.mainBaseActivity = mainBaseActivity;
fragment.inflater = (LayoutInflater) mainBaseActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
fragment.myApplication = ((MyApplication) mainBaseActivity.getApplicationContext());
and use getActivity() in your fragment when you need to access your hosting activity or the Application.
For inflater, one is already passed in as an argument to onCreateView(). No need to fetch it yourself.
(To pass params to a fragment that persist over fragment recreation, use setArguments().)
Fragment has the getActivity() method to retrieve the activity to which it is attached. Use it in place of mainBaseActivity
I'm trying to get the view created in onCreateView () (Fragment) to modify components through findviewbyId but always returns null
eg
this is onCreateView in my fragment
public View onCreateView(final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myLayout, container, false);
return view;
}
and from my activity call fragment and add eg.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment newFragment = new myFragment();
ft.replace(main_container.getId(), newFragment).commit();
/*and i need modify the text button with setText() of View create in my fragment
eg*/
View myViewFragment = newFragment.getView();
Button b = (Button) myViewFragment.findViewById(R.id.mybuttoninfragment);
b.setText("Hello World");
but always returns null, how do I fix this?? Thanks
This is because ft.replace(main_container.getId(), newFragment).commit(); just request an initiation of the fragment transaction, but it will only actually be done (and thus onCreateView() be invoked) the next time the main thread have spare CPU cycles.
So, you'll have to wait calling
View myViewFragment = newFragment.getView();
etc to at a later time (in an onClick() method for example)