Skip to content

Commit

Permalink
03_04e
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgassner committed Jul 21, 2020
1 parent 778f5a5 commit 8d9f148
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/src/main/java/com/example/plainolnotes4/EditorFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.example.plainolnotes4.databinding.EditorFragmentBinding

Expand All @@ -19,11 +23,42 @@ class EditorFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

(activity as AppCompatActivity).supportActionBar?.let {
it.setHomeButtonEnabled(true)
it.setDisplayShowHomeEnabled(true)
it.setDisplayHomeAsUpEnabled(true)
it.setHomeAsUpIndicator(R.drawable.ic_check)
}
setHasOptionsMenu(true)

binding = EditorFragmentBinding.inflate(inflater, container, false)
binding.editor.setText("You selected note number ${args.noteId}")

requireActivity().onBackPressedDispatcher.addCallback(
viewLifecycleOwner,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
saveAndReturn()
}
}
)

return binding.root
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> saveAndReturn()
else -> super.onOptionsItemSelected(item)
}
}

private fun saveAndReturn(): Boolean {
findNavController().navigateUp()
return true
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this).get(EditorViewModel::class.java)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/example/plainolnotes4/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
Expand All @@ -24,6 +25,10 @@ class MainFragment : Fragment(),
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

(activity as AppCompatActivity)
.supportActionBar?.setDisplayHomeAsUpEnabled(false)

binding = MainFragmentBinding.inflate(inflater, container, false)
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)

Expand Down

0 comments on commit 8d9f148

Please sign in to comment.