如何调用popbackstack而不删除backstack上的先前条目,以便我可以再次使用它?

时间:2019-06-15 12:53:45

标签: android android-studio android-layout android-fragments android-fragmentactivity

我有一个自定义日历,该日历在活动顶部显示一周的7天。我希望当用户每天按下时添加具有唯一标签的新片段,以便我可以回叫它,并在用户按下已经按下并放置数据的同一天按用户输入的数据再次显示它。这是我的应用的外观-> http://prntscr.com/o276v6

我尝试使用addtoBackStack(tag)并调用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");
        }
    });

但是使用popbackstack删除了backstack中的先前条目,因此我丢失了数据片段。所以我猜想使用popbackstack是错误的方法,或者我可以在调用popbackstack方法时重写onDestroy之类的某些方法以不删除片段。还是我必须在onSaveInstanceState中做一些事情来保存片段和数据? 我是android新手,使用了一些代码并引用了链接,因此我了解您的代码并学到了一些东西。

2 个答案:

答案 0 :(得分:0)

此方法可以帮助您在执行碎片操作时保留它。

 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);
        }
    }

此处允许查看片段popbackstack

  ft.addToBackStack(null);

答案 1 :(得分:0)

我想回答我的问题,所以如果有人过来,我会帮助别人。 我通过添加,显示和隐藏解决了我的问题,这样片段可以保持活动状态而不会被重新创建。(我不知道这是否是性能的好解决方案)

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;