I have a custom calendar that show 7 days of the week at the top of the activity. I want when the user press at a day to add a new fragment with unique tag so I can call it back and show it again with the data that user put in when the user press at the same date that already press and put data. Here how my app is look -> http://prntscr.com/o276v6
I try that using addtoBackStack(tag) and call popbackstack.
public class CalendarView extends LinearLayout{
...
private FragmentManager manager;
private FragmentTransaction transaction;
private CalendarAdapter mAdapter; //CalendarAdapter is a class that i make to fill the gridview with dates
...
private void setGridCellClickEvents(){ //when a date pressed
calendarGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
manager=((Activity)context).getFragmentManager(); //FragmentManager
HomeFragment selected=new HomeFragment();
int position1=position; //position in gridview 1-7
Object current_date= mAdapter.getItem(position); //getting the selected date from the adapter as object
String tag =formater1.format(current_date); //format the date from the object
boolean backstack_exist =manager.popBackStackImmediate(tag,0);
if (!backstack_exist && manager.findFragmentByTag(tag) == null){
transaction= manager.beginTransaction(); //FragmentTransaction
transaction.replace(R.id.user_frame,selected,"position"+position)
.addToBackStack(tag) // addtobackstack with unique tag e.g 15 06 19
.commit();
}else {
manager.popBackStack(tag,0); //popbackstack that unique tag
}
Log.d(TAG,"Changed");
}
});
But with popbackstack delete previous entries in backstack so I lose fragments with data.So I guess using popbackstack is the wrong way or can I override some methods like onDestroy to not delete the fragments when i call the popbackstack method. Or should I have to do something in onSaveInstanceState to save my fragment and the data? I am new at android using some code and reference to a link so I understand your code and learn something will appreciate.
i want to answer my question so i help someone if come here. I solve my problem with add,show and hide so the fragment stay alive and not get recreat.(i dont know if that is a good solution for the performance)
manager=getFragmentManager(); //FragmentManager
HomeFragment selected=new HomeFragment();
int position1=position; //position in gridview 1-7
Object current_date= parent.getAdapter().getItem(position); //getting the selected date from the adapter as object
tag = formater1.format(current_date); //format the date
Fragment previous_fragment = manager.findFragmentByTag(previous);
if (previous_fragment!=null){
transaction= manager.beginTransaction();
transaction.hide(previous_fragment)
.commit();
}
if ( manager.findFragmentByTag(tag) == null){
transaction= manager.beginTransaction(); //FragmentTransaction
transaction.add(R.id.user_frame,selected,tag)
.commit();
}else {
Fragment my_fragment = manager.findFragmentByTag(tag);
transaction=manager.beginTransaction();
transaction.show(my_fragment)
.commit();
}
previous=tag;
previous_view=view;
previous_position=position;