I started developing android apps the other day, and while studying while looking at the text, I had a question about how to use the addToBackStack method, so I'll write it down.
As the operation of the application, when the item list is selected, the screen transitions to the details screen of the selected item, and It returns to the list when you press the back button.
Use the addToBackStack method to save the state of the return destination. The usage of addToBackStack described in the text is as follows.
//Replace list in detail
fragmentTransaction.replace(R.id.fragment1,myFragment2);
//Save the original transaction for processing when the back button is pressed
fragmentTransaction.addToBackStack(null);
The following two points made me wonder when I saw this code.
The contents of the investigation on the above two points are summarized below.
Looking at the above code, After replacing the Fragment with the fragmentTransaction.replace method The state is saved by the fragmentTransaction.addToBackStack method.
The question here is The question is that if you save after swapping, you may not be able to save the state of the return destination. In other words Fragment replacement ⇒ save not, Save ⇒ Replace Fragment I thought it should be.
So, if you look at the description of addToBackStack in Android Developers,
Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.
Roughly translated, Save the transaction on the stack. It is recorded on the stack after it is committed. It will be reverted when popped off the stack.
Unfortunately I didn't get the answer. After that, I searched for blogs, etc., but couldn't get a clear answer. After that, when I was investigating changing the screen layout on tablets and smartphones, I found the answer. On the fragments description page (https://developer.android.com/guide/components/fragments.html?hl=ja) I found the following description.
If you make multiple changes to a transaction (such as add () or remove ()) and call addToBackStack (), all the changes applied before calling commit () are put into the back stack as one transaction. Once added, select the Back button and everything will come back at the same time.
In other words, the addToBackStack method saves the diff. If you want to save the difference, replace Fragment ⇒ Save.
This is simply the question, "What does it mean to have a null argument?"
This question is a commentary article by Android-Note (http://android-note.open-memo.net/sub/fragment__fragment_stack.html) There was the following description in.
If you don't have multiple fragments to save on the stack and you want to save only the current one, pass null as follows:
I see.
that's all.
Repository https://github.com/tagfa/MyGUIFragmentDynamic02 Where to use the addToBackStack method https://github.com/tagfa/MyGUIFragmentDynamic02/blob/master/app/src/main/java/com/example/tag/myguifragmentdynamic02/MainActivity.java
Recommended Posts