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
// Using R.layout.activity_main | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// Instead of findView(R.id.toolbar) as Toolbar | |
setSupportActionBar(toolbar) |
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 SimpleViewModel() : ViewModel() { | |
/** | |
* 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 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 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
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
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
FileReader("input.txt").use { | |
// Read the file | |
} | |
// Automatically closed |
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
try (FileReader reader = new FileReader("input.txt")) { | |
// Read the file | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
// Automatically closed |
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().also { | |
it.name = "Tony Stark" | |
it.age = 52 | |
// More such stuff | |
} |