Closed ened closed 11 years ago
Did you try to set API level more than Android 4.2?
Nested fragments cannot have setRetainInstance(true) I removed this line in the CaldroidFragment and now it works :)
unfortunately if you remove that you cannot use it as dialog... The way Android handle the states quite messy :(
Yes, it's on both 4.2, 4.3 and 2.3 (target API is always latest, min API is 10). Both with support library and without.
We are using my fork now with the setRetainInstance
call omitted (https://github.com/ened/Caldroid/tree/retain-instance).
@thomasdao I understand your point about the Dialog handling. Do you think the proposal from the SO link is helpful? Plus, starting from API 17 (and in recent support library) there is a method http://developer.android.com/reference/android/app/Fragment.html#getParentFragment(), which might be helpful to make the decision on whether to call setRetainInstance()
or not?
@ened: can clarify a bit more on what you want to achieve? I haven't tried Roboguice so I don't understand your code fully.
In my app, I can include Caldroid within other Fragment as well. Only difference is I extend android.support.v4.app.Fragment
and using SherlockFragmentActivity
Your suggestion is useful but I need time to test it out. Android is not always straightforward :(
@thomasdao basically I'm using a fragment to present the UI to the user. The @AfterViews
call is running just after onCreate
is complete. Within the AnalysisFragment (inherits android.support.v4.app.Fragment
now due to switch from ABS to ActionBarCompat) I'm simply adding the CaldroidFragment into the right place.
Maybe important: This is all done using the latest version of the compatibility library (r18).
Can you try below code:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction t = fragmentManager.beginTransaction();
t.add(R.id.containerCalendar, caldroidFragment);
t.commit();
On a first try (running on 4.2), this works. Per http://developer.android.com/about/versions/android-4.2.html#NestedFragments I'm doing something that really should be done using the child fragment manager though.
I'll try to support getChildFragmentManger
, but in the mean time you can use getSupportFragmentManager
instead. To support both dialog and embedded fragments, sometimes I run into weird issues with states.
Ok, thanks a lot for your help.
I found a simple way to fix this issue: just check if Caldroid is presented as dialog, and we only setRetainInstance(true) for dialog fragment. Pls let me know if it works for you :)
I had the same problem. change FragmentTransaction t = getChildFragmentManager().beginTransaction(); to FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
hello everyone. i tried to embed caldroid in my app, but i still get some errors in my mainactivity. i'm using android 4.3 : here t.replace(R.id.calendar1, caldroidFragment); eclipse suggests me to change type of caldroidFragment from DialogFragment to Fragment. . and here FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction(); getActivity() is showed as an error. any help will be appreciated
show your code, and also the error message plz
thx for the quick reply, but i managed to solve the problem. this is how i fixed it: android.support.v4.app.FragmentTransaction t = getSupportFragmentManager().beginTransaction(); t.replace(R.id.calendar1, caldroidFragment);
but i have another issue. as i embed caldroid in my app, pls can u show me how to implement on date click so that i can pop up a dialog box when i click on any date, and how to sync the calendar to my database so that a date background color change according to some data in my database. thx in advance.
sorry to bother again , but i dont know how to implement it: this is what i tried: // Setup listener listener = new CaldroidListener() {
@Override
public void onSelectDate(final Date date, View view) {
Toast.makeText(getApplicationContext(), formatter.format(date),
Toast.LENGTH_LONG).show();
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), formatter.format(date),
Toast.LENGTH_LONG).show();
}
}); }
Can you open new issue? It's quite hard to track. Btw to show dialog:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create a dialog here
CaldroidFragment dialogCaldroidFragment = CaldroidFragment.newInstance("Select a date", 3, 2013);
// Add a listener as per documentation
final CaldroidListener listener = new CaldroidListener() {
@Override
public void onSelectDate(Date date, View view) {
// You can perform your method on selected date
}
};
dialogCaldroidFragment.setCaldroidListener(listener);
// show the dialog
dialogCaldroidFragment.show(getSupportFragmentManager(),"TAG");
}
});
Btw I think it will be good for you in long term to slow down and understand Android fundamental and do not rush, you can do much faster later
Given I add the CaldroidFragment into an existing fragment, I will receive this error message:
This is how I set things up:
Stackoverflow has some hints: http://stackoverflow.com/questions/14850573/cant-retain-nested-fragments .
Full stacktrace: