why onBackPressed() syntax is closing the app? - android

the onBackPressed() in the signup page is okay and i put the same code in another activity and when i pressed it, the app will go back to previous activity but it will close first..
this is the code in my second activity which the app will close after i press the back arrow
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAllusersignupBinding.inflate(layoutInflater)
setContentView(binding.root)
//action bar
actionBar = supportActionBar!!
actionBar.title = "User Registration"
actionBar.setDisplayHomeAsUpEnabled(true)
}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()//go back to previous activity, when back button of actionbar clicked
return super.onSupportNavigateUp()
}
}

Related

Enable Programatically Back button in AppCompatActivity

I have been meeting this issue now and then, how to make activity show back button an make it return to the parent activity without too much complexity.
Google have extensive documentation, but it is blown for someone who want a simple working approach. So am putting this question and answering it myself as rather documentation for others.
Enable the "Back" button with
supportActionBar?.setDisplayHomeAsUpEnabled(true)
Override onSupportNavigateUp to make it actually go back
override fun onSupportNavigateUp(): Boolean
{
onBackPressed()
return super.onSupportNavigateUp()
}
Here is the full code
class SomeChildActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new_invoice)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return super.onSupportNavigateUp()
}
}

How to navigate back to previous fragment using Navigation component

I am using navigation component in my app.I have three fragment in my app Home, Cart and Cake. I want to navigate to the desired fragment on pressing back arrow in action bar. When I am clicking on back arrow it is not switching to desired fragment rather than going back to Home fragment.
Below is my code:
CartFragment.java
OnBackPressedCallback back = new OnBackPressedCallback(true) {
#Override
public void handleOnBackPressed() {
NavController navController = Navigation.findNavController(getActivity(),R.id.fragment);
navController.navigate(R.id.cakeFragment);
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(getActivity(),back);
To intercept the back button click you need to override onOptionsItemSelected:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
handleBackPress()
return true
}
}
return super.onOptionsItemSelected(item)
}

How to pass extra data with Intent when using options menu's back arrow button?

My "child" activity's options menu has a back arrow button, and my putExtra call isn't correctly saving my ByteArray data "sn" (or else, I'm somehow not passing the intent correctly when the back button is clicked). Here's the child activity's onCreate method:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
Log.d(TAG, "back button was pressed, writing ${sn.toHexString()}")
val intent = Intent()
intent.putExtra(EXTRA_SN, sn)
setResult(REQUEST_CODE_SN, intent)
return super.onOptionsItemSelected(item)
}
The Log.d line shows the data is correct above, and here's the parent activity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// THE BELOW LINE CAUSES AN EXCEPTION:
// getByteArrayExtra(EXTRA_SN) must not be null
sn = intent.getByteArrayExtra(EXTRA_SN)
Log.d(TAG, "sn is now ${sn.toHexString()}")
binding = DataBindingUtil.setContentView(this,
R.layout.activity_foo
)
}
What's going wrong?

How to handle the back button DrawerNavigation on android with kotlin

I'm using the DrawerLayout with fragments inside and every fragment navigates to another fragment. I was able to handle the physical back button using :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
findNavController().navigate(R.id.action_user_validation_to_make_money);
}
}
But every time that I go inside another fragment, the toolbar shows a back button :
I would like to know how can I handle that back button. Thanks!
To navigate using up button, as mentioned in the official docs, override onSupportNavigateUp() in your activity class
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(drawerLayout)
}

Disable multiple instance of fragment

In my main activity when user clicks button it shows him specific fragment. But if he clicks it again it adds another instance to backstack. And then the user needs to click back many times as click on the button.
class AppActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_app)
SharedPreferenceHelper.init(this)
GRPCClient.init(this)
DataBaseHelper.init(this)
imageViewDot.setOnClickListener {
findNavController(this, R.id.navHostFragmentApp).navigate(R.id.syncFragment)
}
}}
How can I prevent it from happening. What I need to do is if fragment is visible the button will not do anything.
try to flag that you already added this fragment and don't do that again
class AppActivity : AppCompatActivity() {
var navigated: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_app)
SharedPreferenceHelper.init(this)
GRPCClient.init(this)
DataBaseHelper.init(this)
imageViewDot.setOnClickListener {
if(navigated) return
findNavController(this, R.id.navHostFragmentApp).navigate(R.id.syncFragment)
navigated = true
}
}
override fun onBackPressed() {
super.onBackPressed() // removes from back stack if present in there
navigated = false
}
}
or you can use getBackStackEntry or getCurrentBackStackEntry from NavController instance to check that this Fragment is already on the position

Categories

Resources