简体   繁体   中英

Android fragment on back transition

I got a few fragments and I tried to sort out the transitions between them. I can go from Main -> A -> B -> C . Then once I am done with the stuffs in C , I wanted to go back to B -> A -> Main . This is the desired transition I wanted to achieve.

However, with my current code, there is something weird with the transition. I go from Main -> A -> B -> C , then inside C I am doing some SQL to create data in database, once I am done, I go back to B . However, from there, when I pressed back button, it go back to C -> B -> A -> Main . There is an unnecessary C in the back transition.

Inside my Main , I am calling A like this:

final SettingActivity settingFragment = new SettingActivity();
ft.replace(R.id.frame,settingFragment);
ft.addToBackStack("tag");
ft.commit();

Inside my A , I am calling B like this:

FragmentTransaction it = getFragmentManager().beginTransaction();
it.replace(R.id.frame,categoryFragment);
it.addToBackStack("tag");
it.commit();

Inside my B , I am calling C like this:

FragmentTransaction et = getFragmentManager().beginTransaction();
et.replace(R.id.frame,editFragment);
et.addToBackStack("tag");
et.commit();

Then inside my C , when I am successfully inserted a record in database, I am calling B like this:

// button on click listener
new GetAllCategoriesAsyncTask(
            new GetAllCategoriesAsyncTask.OnRoutineFinished() {
                public void onFinish() {

                    Bundle bundle = new Bundle();
                    FragmentTransaction it = getFragmentManager().beginTransaction();

                    bundle.putSerializable("categorylist", GetAllCategoriesAsyncTask.categoryList);
                    categoryFragment.setArguments(bundle);
                    it.replace(R.id.frame,categoryFragment);
                    it.addToBackStack("tag");
                    it.commit();
                }
            }).execute();

Then inside my B , I am getting the data like this in onCreate():

categoryList = (ArrayList<Category>) getArguments().getSerializable("categorylist");

On button click pop the current fragment from stack which is C , instead of adding B again in the stack. So, replace the button onClick code with below line of code:

getFragmentManager().popBackStack();

Bundle is used when you transferring data to next screen. For transferring data to previous screen you need to use callbacks.

For reference please find below attached link : Communicating with Other Fragments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM