Skip to content

Commit

Permalink
Issue mozilla-mobile#35: Handle device back button
Browse files Browse the repository at this point in the history
  • Loading branch information
csadilek committed May 4, 2018
1 parent 6ec93d2 commit 793bbad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
32 changes: 22 additions & 10 deletions app/src/main/java/mozilla/fenix/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,38 @@ import mozilla.fenix.components.FeatureLifecycleObserver
import mozilla.fenix.ext.components

class MainActivity : AppCompatActivity() {
private var toolbarFeature: ToolbarFeature? = null
private var sessionFeature: SessionFeature? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val toolbarFeature = ToolbarFeature(
components.sessionManager,
components.sessionUseCases.loadUrl,
toolbar)
toolbarFeature = ToolbarFeature(
components.sessionManager,
components.sessionUseCases.loadUrl,
toolbar)

val sessionFeature = SessionFeature(
components.sessionManager,
components.sessionUseCases,
components.engine,
engineView,
components.sessionMapping)
sessionFeature = SessionFeature(
components.sessionManager,
components.sessionUseCases,
components.engine,
engineView,
components.sessionMapping)

lifecycle.addObserver(FeatureLifecycleObserver(sessionFeature, toolbarFeature))
}

override fun onBackPressed() {
if (toolbarFeature?.handleBackPressed() == true)
return

if (sessionFeature?.handleBackPressed() == true)
return

super.onBackPressed()
}

override fun onCreateView(parent: View?, name: String?, context: Context?, attrs: AttributeSet?): View? {
if (name == EngineView::class.java.name) {
return components.engine.createView(context!!, attrs).asView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ import mozilla.components.feature.toolbar.ToolbarFeature
* LifecycleObserver implementation that will forward lifecycle callbacks to "feature" components.
*/
class FeatureLifecycleObserver(
private val sessionFeature: SessionFeature,
private val toolbarFeature: ToolbarFeature
private val sessionFeature: SessionFeature?,
private val toolbarFeature: ToolbarFeature?
): LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun startFeatures() {
sessionFeature.start()
toolbarFeature.start()
sessionFeature?.start()
toolbarFeature?.start()
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stopFeatures() {
sessionFeature.stop()
toolbarFeature.stop()
sessionFeature?.stop()
toolbarFeature?.stop()
}
}

0 comments on commit 793bbad

Please sign in to comment.