linussarode

Untitled

May 18th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.93 KB | None | 0 0
  1. /*I have cart and confirm option in my onCreateOptionsMenu of activity, confirm option is not visible
  2.  
  3. now when I click on cart I am replacing activity or current fragment on activity with cartfragment.below is  onOptionsItemSelected from activity */
  4.  
  5.  
  6.  override fun onOptionsItemSelected(item: MenuItem): Boolean {
  7.         return when (item.itemId) {
  8.             R.id.action_cart -> {
  9.              
  10.  
  11.                 val myFragment =supportFragmentManager.findFragmentById(R.id.placeholder)
  12.  
  13.                 if (myFragment != null && myFragment is CartFragment) {
  14.  
  15.                     Toast.makeText(applicationContext,"Already in Cart",Toast.LENGTH_SHORT).show()
  16.  
  17.                     return true
  18.                 }
  19.  
  20.  
  21.                 var frag=CartFragment()
  22.                 supportFragmentManager.beginTransaction().replace(R.id.placeholder, frag,"CART").addToBackStack(null).commit()
  23.  
  24.                 true
  25.             }
  26.  
  27.             else -> super.onOptionsItemSelected(item)
  28.         }
  29.     }
  30.  
  31. /*
  32. now in cartfragment  i am making confirm option visible,on click of this confirm option I am replacing cartfragment with finalCartFragment. */
  33.  
  34.     override fun onOptionsItemSelected(item: MenuItem): Boolean {
  35.         val id = item.itemId
  36.         when (id) {
  37.             R.id.order -> {
  38.                 //update the cart
  39.  
  40.                 dbh = DataBaseHandler(activity?.applicationContext!!)
  41.                 dbh!!.updateCartToFinalView(cartList)
  42.                 var fragment: FinalCartFragment = FinalCartFragment()
  43.                 activity?.supportFragmentManager?.beginTransaction()
  44.                     ?.replace(R.id.placeholder, fragment)?.addToBackStack(null)?.commit()
  45.                 return true
  46.             }
  47.  
  48.  
  49.         }
  50.  
  51.         return false
  52.     }
  53.  
  54. /*now inside the finalCartFragment , the cart option is still there, and when I click on that it will take me to cartfragment again ,all good (it is getting call from activities onOptionsItemSelected).
  55. now when I do backpress, I go back to finalCartFragment, I don't want this to happen.
  56.  
  57. I tried following in my finalCartFragment , but it is not working */
  58.  
  59.  setHasOptionsMenu(true) in onCrete..and following
  60.  
  61.  
  62.     override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
  63.         menu.clear()
  64.         inflater.inflate(R.menu.menu_item, menu);
  65.  
  66.         val order = menu.findItem(R.id.order)
  67.         order?.setVisible(true)
  68.  
  69.     }
  70.  
  71.     override fun onOptionsItemSelected(item: MenuItem): Boolean {
  72.         val id = item.itemId
  73.         when (id) {
  74.             R.id.order ->{
  75.                 //show dialog
  76.                 showdialog()
  77.             }
  78.             R.id.action_cart->{
  79.                 var fragment=CartFragment()
  80.                 activity?.supportFragmentManager?.beginTransaction()
  81.                     ?.replace(R.id.placeholder, fragment)?.commit()
  82.  
  83.                 return true
  84.             }
  85.  
  86.  
  87.         }
  88.  
  89.         return false
  90.     }
Add Comment
Please, Sign In to add comment