This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val person = Person().apply { | |
name = "Tony Stark" | |
age = 52 | |
// More such stuff | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person?.let { | |
// person object is not null in here | |
it.name = "Tony Stark" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with(Person()) { | |
name = "Tony Stark" | |
age = 52 | |
// More such stuff | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person person = new Person(); | |
person.setName("Tony Stark"); | |
person.setAge(52); | |
// More such stuff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val executionTime = measureTimeMillis { | |
// Do your task | |
} | |
println("Execution Time = $executionTime ms") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
long startTime = System.currentTimeMillis(); | |
// Do your task | |
long executionTime = System.currentTimeMillis() - startTime; | |
System.out.println("Execution Time = " + executionTime + " ms"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ViewModelStore { | |
private final HashMap<String, ViewModel> mMap = new HashMap(); | |
public ViewModelStore() {} | |
final void put(String key, ViewModel viewModel) { | |
ViewModel oldViewModel = (ViewModel)this.mMap.get(key); | |
if(oldViewModel != null) { | |
oldViewModel.onCleared(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : LifecycleActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// Get the ViewModel instance | |
val simpleViewModel = ViewModelProviders.of(this).get(SimpleViewModel::class.java) | |
val data = simpleViewModel.getDataList() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ContextViewModel(application: Application) : AndroidViewModel(application) { | |
/** | |
* The data | |
*/ | |
private var data: List<Data> | |
init { | |
// Load the data over here | |
// data = .... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleViewModel() : ViewModel() { | |
/** | |
* The data | |
*/ | |
private var data: List<Data> | |
init { | |
// Load the data over here | |
// data = .... |