I Created a Subclass of Button to get a Button with some extra functionality.
In this subclass I created an innerclass which implements OnClickListener to use it as an Onclicklistener for this Buttonclass.
There are three buttons of this class which are added to my MainActivity. Whenever I click on this Button I want to change a Fragment on in the same Activity.
So I need to get a Fragmentmanager in my Button- or OnclickListener-Subclasses.
What is the best Way to achieve this?
A Way I could imagine is to create a setter in my button to set a fragment manager from my MainActivity. But I don't think this is a very good way.
You can do something like this. Where context is the context that is passed in your button's constructor.
FragmentTransaction fragmentTransaction = ((Activity) context).getSupportFragmentManager().beginTransaction();
fragmentTransaction.addToBackStack(null);
Fragment frag = new YourFragment();
fragmentTransaction.replace(R.id.your_content_frame, frag);
fragmentTransaction.commit();
Related
I have added a fragment in another fragment
in this way
Secondfragment secondFragment=new SecondFragment(parmater);
fragmentManager.beginTransaction()
.replace(fragmentContainerView.getId(),
secondFragment, null)
.setReorderingAllowed(true)
.commit();
the firstFragment is containing the fragmentContainerView and it's inside a ViewPager2 with other fragments
after changing the page and return to it again the app is crashes and give me the error
Unable to instantiate fragment , as I mentioned this because the firstFragment have a parameter in constructor .
I use this way in multiple places .
after changing the page the first fragment is been in the onPause state and after I go to the first fragment again its recreate the views and the proplen happen here , it cannot instantiate the second fragment.
I want a way to let it instantiate it again ,or any better idea and thank you in advance .
I have an application which has a main activity and several fragments which are navigated to via a navigation drawer.
I use two map fragments in my app, this seems to cause an issue if i go from fragment map A and then back to fragment map B . In frgamnet b i lose control of them map is generally just shows a snapshot of where fragmnet map A last was.
this seem to be a known issue see.
google maps api bug report with issue
My solution as sugested on abouve fourm is to minimive map A before loading map B this causes the issue not to happen as it will not take up the screen space.
public void hideStupidMaps() {
mMapView.getLayoutParams().height = 1;
mMapView.getLayoutParams().width = 1;
mMapView.invalidate();
mMapView.requestLayout();
}
The above method is in my Gmap fragment class. i want to call it from myMain Activity class. in the navagation drawer code. My question is how do i call a frgments method from the main activity. especally when altering that fragment view. i need to inflate the view and it to the above method?
You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle.
To return a layout from onCreateView(), you can inflate it from a layout resource defined in XML. To help you do so, onCreateView() provides a LayoutInflater object.
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
Here's how you can replace one fragment with another, and preserve the previous state in the back stack:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
I've been doing it like this for a while, is it the right way?
Activity:
public void toSettings(){
Fragment_Settings frag = new Fragment_Settings();
fm.beginTransaction()
.replace(R.id.mainContainer, frag)
.addToBackStack(null)
.commit();
}
In some listener in another fragment:
((Activity_Main)getActivity()).toSettings();
You don't need .addToBackStack(null) Just delete that piece and your fragment will not be added to backstack. I would also suggest adding a TAG to the replace method just in case you want to retrieve the fragment later. Something like this .replace(R.id.mainContainer, frag, TAG).
It seems correct but i have some suggestions.
If you wanna back previous fragment from Fragment_Settings use add method instead of replace and addToBackStack("tag of Fragment_Settings") instead of addToBackStack(null).
newInstance pattern is more preferred than Fragment_Settings frag = new Fragment_Settings();. You can see some explanations about that.
http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html
Creating a Fragment: constructor vs newInstance()
To avoid code repetition when navigating between fragments you can use a manager class provides organizing add, replace or pop processes.
Here is my navigation manager class.
So currently I have a application with different activities, and I use buttons to navigate between activities. I later decided that I should add the navigation drawer and use fragments instead. So one of my fragments has a bunch of fields that the user fills out that I need to pass onto the next fragment. So my question is, Do I keep all the work in activities and call the activity from the fragment? Or do I just include all he java in my activity in the java for the fragment? For the most part I am taking the fields from the first fragment, and I'd like to pass the values to the next fragment so it can handle some calculations.
final EditText FinalPriceText = (EditText) v.findViewById(R.id.editTextPrice);
final EditText TradeInPriceText = (EditText) v.findViewById(R.id.editTextTrade);
i.putExtra("FinalAutoPrice", FinalAutoPriceText.getText().toString());
i.putExtra("TradeInPrice", TradeInPriceText.getText().toString());
startActivity(i);
As far as calculations go, if you are going to use them a lot and they have nothing to do with the activity or android lifecycle I would separate them out into a different class and then you can call them from anywhere.
If they do rely on the activity you could still separate them out but pass a reference to the activity when doing your calculations. You can get the parent activity by calling this.getActivity() from any fragment
You can cast this.getActivity() to whatever the parent activity is and you can call the methods from that object as well. This works fine but your fragment will only work with the activity you specify and it can get sloppy if you are not careful.
Otherwise put them in the fragment where you need them. I would consider this least recommended if you need to use calculations anywhere else in the app. Duplicate code is just asking for bugs in the future.
As far as passing data, create a static instance method in fragment2 and pass it what you need there.
For example
public Fragment2 extends Fragment {
public static fragment2 newInstance(MyData myDataIPass) {
Fragment2 fragment = new Fragment2();
Bundle args = new Bundle();
args.putInt("someInt", myDataIPass.someInt);
fragment.setArguments(args);
return fragment;
}
}
Call this new instance method when creating your fragment transaction like this
FragmentManager fm = getActivity().getFragmentManager();
fm.beginTransaction()
.replace(R.id.container, Fragment2.newInstance(MyData myDataIPass))
.commit();
Well, you have mainly two options here:
use Intent.putExtra() with fragments. Just like Activities, you
can use this method with Fragments as well. See the following links
for the implementation in Fragments
this
this
The other option is to use SharedPreferences and store data as key value pairs from one fragment, and can be accessed from any other activity/fragments. See this nice tutorial to understand better!
You have a special callback in Fragment to get Activity.
It is called:
#Override
public void onAttach(Context context) {
super.onAttach(context);
YourActivity activity = (YourActivity) context;
}
I have an activity which extends SherlockFragment
I am trying to obtain the fragmentmanager object
I get a nullpointer exception on this line
FragmentManager fragmentManager = getSherlockActivity().getSupportFragmentManager();
basically this activity itself is a fragment and i want to change the activity on the framelayout to some other fragment.
Thanks!
Why not use getFragmentManager()? Its listed in the methods list of Fragment in the support library, and returns the manager from this fragment's containing activity. getSupportFragmentManager() is the method used in the FragmentActivity, not in Fragment.
Your question isn't very clear, but this seems like what you want to do, no?
http://developer.android.com/reference/android/support/v4/app/Fragment.html#getFragmentManager()