[JAVA] About call timing and arguments of addToBackStack method

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.

  1. Method call timing
  2. Method arguments

The contents of the investigation on the above two points are summarized below.

1. Method call timing

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.

2. Method arguments

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.

github link

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

About call timing and arguments of addToBackStack method
[Ruby] Questions and verification about the number of method arguments
About fastqc of Biocontainers and Java
About the role of the initialize method
About removeAll and retainAll of ArrayList
Overload method with Int and Integer arguments
About the operation of next () and nextLine ()
About the mechanism of the Web and HTTP
Think about the combination of Servlet and Ajax
[Ruby] Arguments with keywords and default values of arguments
[Java] Dynamic method call by reflection of enum (enum)
About next () and nextLine () of the Scanner class
Summary of Japan time setting and display method
Check method call arguments in blocks with RSpec
About characters that are completed in method arguments
About keyword arguments
About the method
Pass arguments to the method and receive the result of the operation as a return value
variable and method
[Grails] About the setting area and the setting items of application.yml
[Java] About Objects.equals () and Review of String comparisons (== and equals)
[Technical memo] About the advantages and disadvantages of Ruby
Be careful about Ruby method calls and variable references
[Java] [Kotlin] Generically call valueOf and values of Enum