I'm searching for a way to create a new Kotlin live code template so that whenever I type newIns.... it and hit tab it will be able to print the following as a live template choice:
companion object {
fun newInstance(b: Bundle): DetailsFragment {
val frag = DetailsFragment()
frag.arguments = b
return frag
}
}
In Java, it was done the same way and there is already a "newInstance" abbreviation and a live template exists in Android Studio. I want the same thing for Kotlin. Let me show you a photo :
Notice that, Java Android already has newInstance
template. I want this for Kotlin. Here is what I have so far:
and the template code I have so far looks like this:
companion object {
fun newInstance($args$:Bundle):$fragment$ {
$nullChecks$
android.os.Bundle args = Bundle();
$addArgs$
$fragment$ fragment = $fragment$();
fragment.setArguments(args);
return fragment;
}
}
but when I exit the settings and in Kotlin type the first few words of the abbreviation and hit tab or ctrl + spacebar on mac nothing happens. I guess I have the syntax wrong, I'm not sure. Anyone of suggestions?
The best practice on Android for creating a Fragment is to use a static factory method and pass arguments in a Bundle via setArguments() . This makes sense to support interop with Java so it can still be called via MyFragment.
Live templates are a feature that JetBrains thoughtfully included in IntelliJ, and by extension, Android Studio. By using them, you can quickly and intelligently add frequently used code patterns and constructs to your code, letting the IDE do the tedious work for you.
Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();
To create a blank Fragment , expand app > java in Project: Android view, select the folder containing the Java code for your app, and choose File > New > Fragment > Fragment (Blank).
Step 1:
Go to Live Templates section in Android Studio.
For Windows:
File > Settings > Editor > Live Templates
For Mac:
Android Studio > Preferences > Editor > Live Templates
Step 2:
Select Kotlin template group. Then tap on +
present at the top-right corner of the popup. Select Live Template.
Step 3:
Now you can add your live template. Check at the bottom of the popup.
Add abbreviation: newInstance
Add description: Creates an instance of the fragment with arguments
Add template text:
companion object {
fun newInstance(args: Bundle): $fragment$ {
val fragment = $fragment$()
fragment.arguments = args
return fragment
}
}
Add applicable context. Tap on Define
. Select Kotlin from the list.
Select Reformat according to style
Step 4:
Tap on Edit Variables below the description.
Now tap on Expression for the variable name fragment
. Tap on down arrow. You can see a list of expressions. From there select kotlinClassName()
.
Tap on OK of the Edit Templates Variable
Now tap on Apply and OK of the Live Templates.
To check type newInstance
in a Fragment written in Kotlin.
A slightly simplified version (and more idiomatic) would be:
fun newInstance($args$) : $fragment$ =
$fragment$().apply {
arguments = Bundle().apply {
$addArgs$
}
}
or
fun newInstance(args: Bundle) : $fragment$ =
$fragment$().apply {
arguments = args
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With