Skip to content

Instantly share code, notes, and snippets.

View wajahatkarim3's full-sized avatar
:octocat:
Subscribe to my newsletter: remotekaro.substack.com

Wajahat Karim wajahatkarim3

:octocat:
Subscribe to my newsletter: remotekaro.substack.com
View GitHub Profile
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.search_menu, menu)
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Search"
android:icon="@drawable/ic_baseline_search_24"
app:showAsAction="always"
android:iconTint="@color/colorWhite"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bottom_nav_graph.xml"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.wajahatkarim3.bottomnavigationdemo.HomeFragment"
class NotificationsFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_notifications, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupViews()
fun showBottomNavigation()
{
bottomNavView.visibility = View.VISIBLE
}
fun hideBottomNavigation()
{
bottomNavView.visibility = View.GONE
}
override fun onAttach(context: Context) {
super.onAttach(context)
(activity as MainActivity).hideBottomNavigation()
}
override fun onDetach() {
(activity as MainActivity).showBottomNavigation()
super.onDetach()
}
private var backPressedOnce = false
override fun onBackPressed()
{
var navController = findNavController(R.id.fragNavHost)
// Check if the current destination is actually the start sestination (Home screen)
if (navController.graph.startDestination == navController.currentDestination?.id)
{
// Check if back is already pressed. If yes, then exit the app.
@wajahatkarim3
wajahatkarim3 / MainActivity.kt
Created May 1, 2020 22:00
The onActivityResult() method example
class MainActivity : AppCompatActivity()
{
// Other methods of your Activity
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Capture Image with Camera
if (requestCode == CAMERA_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
if (data != null) {
implementation 'androidx.activity:activity-ktx:1.2.0-alpha04'
implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha04'