|
package lavanderia.org.lavanderia |
|
|
|
import android.content.Context |
|
import android.content.Intent |
|
import android.os.Bundle |
|
import android.support.v4.app.NavUtils |
|
import android.support.v4.app.TaskStackBuilder |
|
import android.support.v7.app.ActionBarDrawerToggle |
|
import android.support.v7.app.AppCompatActivity |
|
import android.view.MenuItem |
|
import lavanderia.org.lavanderia.UtilityClass.Companion.GO_BACK |
|
import lavanderia.org.lavanderia.UtilityClass.Companion.GO_PARENT |
|
import lavanderia.org.lavanderia.UtilityClass.Companion.GO_UP |
|
|
|
class MainActivity : AppCompatActivity() { |
|
lateinit var drawerToggle: ActionBarDrawerToggle |
|
override fun onCreate(savedInstanceState: Bundle?) { |
|
super.onCreate(savedInstanceState) |
|
setContentView(R.layout.activity_main) |
|
} |
|
|
|
override fun onOptionsItemSelected(menuItem: MenuItem): Boolean { |
|
when (menuItem.itemId) { |
|
android.R.id.home -> { |
|
UtilityClass.hideKeyboard(MainActivity@ this) |
|
if (!drawerToggle.onOptionsItemSelected(menuItem)) { |
|
if (supportFragmentManager.backStackEntryCount <= 0) { |
|
navigateBack() |
|
} else { |
|
onBackPressed() |
|
} |
|
return true |
|
} |
|
} |
|
} |
|
return super.onOptionsItemSelected(menuItem) |
|
} |
|
|
|
private fun navigateBack() { |
|
val parentIntent = NavUtils.getParentActivityIntent(this) |
|
when (determineNavBack(parentIntent)) { |
|
GO_PARENT -> { |
|
TaskStackBuilder.create(this) |
|
.addNextIntentWithParentStack(parentIntent as Intent) |
|
.startActivities() |
|
overridePendingTransition(0, 0) |
|
} |
|
GO_UP -> { |
|
NavUtils.navigateUpTo(this, parentIntent as Intent) |
|
overridePendingTransition(0, 0) |
|
} |
|
GO_BACK -> { |
|
onBackPressed() |
|
} |
|
} |
|
} |
|
|
|
private fun determineNavBack(parentIntent: Intent?) = |
|
if (parentIntent != null && NavUtils.shouldUpRecreateTask(this, parentIntent)) { |
|
GO_PARENT |
|
} else if (parentIntent != null) { |
|
GO_UP |
|
} else { |
|
GO_BACK |
|
} |
|
|
|
} |
|
|
|
class UtilityClass { |
|
companion object { |
|
fun hideKeyboard(context: Context) { |
|
TODO("not implemented") |
|
} |
|
|
|
const val GO_PARENT: Int = 0 |
|
const val GO_UP: Int = 1 |
|
const val GO_BACK: Int = 2 |
|
} |
|
} |