[JAVA] Technical causes and countermeasures for points addicted to Android apps & Kotlin (1. Android screen transition (fragment) processing)

1. 1. * Android * screen transition (fragment) processing

In the initial version of * Android *, there was no concept of fragment, and it seems that it was difficult to control fat activity and terminal rotation. In this application, the fragment configuration for each screen is replaced from the main activity, and the control is omitted by prohibiting the terminal rotation in the setting of * AndroidManifest.xml *. The possibility that * UX * will be impaired by prohibiting terminal rotation has been addressed by incorporating a process to rotate the image. (I'm addicted to it later ...)

1.1 Process of pasting the image after screen transition

In this application, in order to seamlessly combine two images, select the original image to be combined (** want to ) on the first image, and combine ( want to **) the destination image on the second image. Choose. (The state of two images is managed by * FragmentA * and * FragmentB *) Therefore, after touching the image selection button to take a picture of the gallery or the camera, the image is pasted after the screen transition.

After touching the image selection button to take a picture of the gallery or the camera, the image is not displayed in * ImageView * on the screen transition destination layout and an exception occurs.

In order to display the image in * ImageView * on the screen transition destination layout, the fragment loaded in the transaction queue is executed immediately, so * FragmentManager * fragment transaction processing (* commit () *) immediately after * By calling executePendingTransactions () *, * onCreateView () * will be called and the layout will be generated.

Fragment transaction processing and executePendingTransactions()


// .....Abbreviation
	FragmentA.newInstance().let { fragment ->
		fragmentManager?.beginTransaction().let { trans ->
			val fragments = fragmentManager?.fragments ?: return
			for (rFragment in fragments) {
				trans?.remove(rFragment)
			}
			trans?.replace(R.id.topContainer, fragment, FragmentA.TAG)
			trans?.commit()
			fragmentManager?.executePendingTransactions()
			fragment.setImageView(intent) //Call after executing all transaction queues
		}
	}
// .....Abbreviation

1.2 Animation image display processing during seamless image composition after screen transition

In this application, two image images (combined (** want ) original image and composite ( want **) destination image) are selected, and the image images are seamlessly composited after the screen transition. Since there is a difference in the composition time depending on the image size, adjust the time in the animation image display.

After blocking until the seamless composition of the image image is completed on * launch * (separate thread) of Kotlin * coroutine while displaying the animation image, it is not possible to transition to the seamless composition result screen by fragment transaction processing.

There was an error in the blocking timing for the main (* UI *) thread in the control until the transition to the seamless composition result screen, and the transition to the seamless composition result screen could not be performed by fragment transaction processing.

While displaying the animation image, it will be blocked until the seamless composition of the image image is completed on * launch * (separate thread) of * Kotlin * coroutine, and then the transition to the seamless composition result screen will be performed in * Activity # runOnUiThread *.

Coroutine launch block processing


// .....Abbreviation
	GlobalScope.launch { // Kotlin1.3 correspondence
		launch { //GlobalScope optional
			asyncFunc(argument).join()
		}.join()
		activity?.runOnUiThread {
			//Fragment transaction processing
		}
	}
// .....Abbreviation

Suspend function


// .....Abbreviation
	private fun asyncFunc(argument): Job {
		return GlobalScope.launch { // Kotlin1.3 correspondence
			seamlesscollage(argument)
		}
	}
// .....Abbreviation

1.3 Seamless composition result Back stack processing on the screen

In this application, touch the [Home] button on the seamless composition result screen to move to the home (top) screen.

After touching the [Home] button on the seamless composition result screen and back stacking, the image selection is not initialized.

Since * addToBackStack () * was called in the fragment transaction processing for the back stack, if you touch the [Home] button (call * popBackStack () *) on the seamless composition result screen, the number of times the fragment transaction was used and the back stack The status such as the screen transition status was not initialized because the number of entries in is not the same.

onViewCreated()When[home]Button processing


// .....Abbreviation
	override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
		super.onViewCreated(view, savedInstanceState)
// .....Abbreviation
		homeButton.setOnClickListener {
			backHome()
		}
	}

	private fun backHome() {
// .....Abbreviation
		replaceTopFragment()
	}
// .....Abbreviation

Fragment transaction processing (replace)())


// .....Abbreviation
	private fun replaceTopFragment() {
		TopFragment.newInstance().let { fragment ->
			fragment.arguments?.putInt(TopFragment.FLG_VIEW, 0)
			fragmentManager?.beginTransaction().let { trans ->
				trans?.replace(R.id.topContainer, fragment, TopFragment.TAG)
				trans?.commit()
			}
		}
	}
// .....Abbreviation

Link

Technical causes and countermeasures for the first Android app & Kotlin addiction 2. Processing related to Android camera function 3. Processing related to Android images

Recommended Posts

Technical causes and countermeasures for points addicted to Android apps & Kotlin (1. Android screen transition (fragment) processing)
Technical causes and countermeasures for points addicted to Android apps & Kotlin (3. Processing related to Android images)
Technical causes and countermeasures for the points I was addicted to with the first Android app & Kotlin
Technical causes and countermeasures for the points that I was addicted to with the Android app & Kotlin (2. Processing related to the camera function of Android *)
[Android / Java] Screen transition and return processing in fragments
Convert all Android apps (Java) to Kotlin
Summary of good points and precautions when converting Java Android application to Kotlin
Specify Java / Kotlin compile-time options for Android apps