Skip to content

Instantly share code, notes, and snippets.

@toidv
Last active June 1, 2017 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toidv/e53f1d87cec5eb10bb1c69834b91ba25 to your computer and use it in GitHub Desktop.
Save toidv/e53f1d87cec5eb10bb1c69834b91ba25 to your computer and use it in GitHub Desktop.
package com.badoo.myapplication
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.v4.app.NavUtils
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import org.jetbrains.anko.sdk25.coroutines.onClick
/**
* An activity representing a single Item detail screen. This
* activity is only used narrow width devices. On tablet-size devices,
* item details are presented side-by-side with a list of items
* in a [ItemListActivity].
*/
class ItemDetailActivity : AppCompatActivity() {
lateinit var fab: FloatingActionButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_item_detail)
val toolbar = findViewById(R.id.detail_toolbar) as Toolbar
setSupportActionBar(toolbar)
fab = findViewById(R.id.fab) as FloatingActionButton
// fab.setOnClickListener { view ->
// Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show()
// }
// Show the Up button in the action bar.
val actionBar = supportActionBar
actionBar?.setDisplayHomeAsUpEnabled(true)
// savedInstanceState is non-null when there is fragment state
// saved from previous configurations of this activity
// (e.g. when rotating the screen from portrait to landscape).
// In this case, the fragment will automatically be re-added
// to its container so we don't need to manually add it.
// For more information, see the Fragments API guide at:
//
// http://developer.android.com/guide/components/fragments.html
//
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
val arguments = Bundle()
arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
intent.getStringExtra(ItemDetailFragment.ARG_ITEM_ID))
val fragment = ItemDetailFragment()
fragment.arguments = arguments
supportFragmentManager.beginTransaction()
.add(R.id.item_detail_container, fragment)
.commit()
}
println(tryToCountButtonClicks(fab))
fab.performClick()
}
fun tryToCountButtonClicks(button: FloatingActionButton): Int {
var clicks = 0
button.onClick {
println(++clicks)
}
return clicks
}
override fun onBackPressed() {
// super.onBackPressed()
println(tryToCountButtonClicks(fab))
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpTo(this, Intent(this, ItemListActivity::class.java))
return true
}
return super.onOptionsItemSelected(item)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment