Skip to content

Instantly share code, notes, and snippets.

@illuzor
Last active September 15, 2018 20:51
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 illuzor/fb5939a97a6ca0208a3c0a520dc1cc4f to your computer and use it in GitHub Desktop.
Save illuzor/fb5939a97a6ca0208a3c0a520dc1cc4f to your computer and use it in GitHub Desktop.
package com.illuzor.lesson.wallpapers.screens
import android.os.Bundle
import android.view.MenuItem
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import com.illuzor.lesson.wallpapers.R
abstract class AbstractActivity : AppCompatActivity() {
protected abstract val layoutId: Int
protected abstract fun getFragment(): Fragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(layoutId)
var fragment = supportFragmentManager.findFragmentById(R.id.container)
if (fragment == null) {
fragment = getFragment()
supportFragmentManager.beginTransaction()
.add(R.id.container, fragment)
.commit()
}
}
protected fun setToolbar(@IdRes id: Int, title: String = "", backButton: Boolean = true) {
val toolbar: Toolbar = findViewById(id)
toolbar.title = title
setSupportActionBar(toolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(backButton)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) finish()
return super.onOptionsItemSelected(item)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment