0

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.

iDeveloper
  • 1,699
  • 22
  • 47

2 Answers2

0

This Method may helps you to keep Fragment when doing it.

 public static void SetFragment(Fragment mFragment, Context mcontext,String title)
    {
        String backStateName = mFragment.getClass().getName();
        FragmentTransaction ft = masterFragmentManager.beginTransaction();
        ft.replace(R.id.content_frame, mFragment,backStateName);
        ft.addToBackStack(null);
        ft.commit();

        if (((AppCompatActivity)mcontext).getSupportActionBar() != null) {
            ((AppCompatActivity)mcontext). getSupportActionBar().setTitle(title);
        }
    }

Here to allow to view Fragment popbackstack

  ft.addToBackStack(null);
Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45
  • How is that gonna work when I dont pass tag to addToBackstack and I pass null? How I am gonna call the popBackStack ? And if you see my code i have one fragment and I want to use it multiple times and go back to the specific fragment if exist with the data that user add before if not replace with new one. The problem is I lose the data if I pass null from fragment that exist. Sorry but that confuse me – Andreas Xatzios Hadjiconstanti Jun 19 '19 at 15:37
  • @AndreasXatziosHadjiconstanti https://stackoverflow.com/questions/14971780/how-to-pop-fragment-off-backstack – Ankitkumar Makwana Jun 20 '19 at 10:16
  • Thnx for your response! I saw that question i did that in my code if you see my code but the problem with popBackStack(tag) method will popped all the state up to that tag. e.x if user press 11 day and put data, then press at 12 day and put data, then at 13 day and put data if he press to 11 day to see his data the data on 11 day is show up ok but popbackstack dalete 12 day and 13 day from backstack and replace with new one so i lose the data(see the image on my quenstion to understand what i say). – Andreas Xatzios Hadjiconstanti Jun 20 '19 at 11:01
  • https://www.codexpedia.com/android/store-and-retrieve-fragment-state-data-in-android/ – Ankitkumar Makwana Jun 20 '19 at 11:06
0

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;