Is it possible to invoke a back button click programmatically? - java

I've seen plenty of answers explaining how to override and add functionality to a back button press via user's interaction, but almost none of the answers explain how to programmatically trigger a back button press without any user input. Answers that do address it give somewhat deprecated code samples that don't really compute.

From your activity just call => onBackPressed(),
or form your fragment => activity?.onBackPressed()

//simply put this code where you want to make back intent
super.onBackPressed();

According to official docs onBackPressed is deprecated in Api level 33.
you can now use onBackPressedDispatcher please follow these steps:
Add android:enableOnBackInvokedCallBack="true” inside application tag
in manifest folder.
Kotlin code :
class YourActivity : AppCompatActivity() {
lateinit var button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button = findViewById(R.id.txt)
button.setOnClickListener {
onBackPressedCallback.handleOnBackPressed()
}
}
private val onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
finish() //this finishes the current activity
// Your business logic to handle the back pressed event
}
}
}
And here is old way of implementation:
Java :
buttonBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourActivity.super.onBackPressed();
}
});
Kotlin :
buttonBack.setOnClickListener{
super.onBackPressed()
}

Related

How to reload an activity once when it open

I want to go from Activity1 to Activity1 (with newly loaded data), but the problem is that when I use recreate(); in onCreate(); the activity just keeps reloading and reloading till the program crashes.
So is there anyway to make the activity reload one time when its opened (By onCreate and onResume).
Clarification: I need to reload the activity to get the new data from a web service (due to a problem that caused the old data to show instead of the new ones) so the only way this will work is by reloading my activity.
Add a flag variable inside your activity ("Instance Variable")
var mIsInitialLoad = true
, then In whatever lifecycle method or function inside your activity:
if (mIsInitialLoad) {
recreate()
mIsInitialLoad = false;
}
You can check this link
Lifecycle State (Android Developers Site)
Example
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
// Call Method to check data
}
package com.note.principal
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import com.note.R
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Check the data when activity is starting
}
override fun onResume() {
super.onResume()
// Check the data when activity finishes
}
}

Pass data by pressing tab in android

I wonder how to pass data by pressing tab. I have two tab in activity, named Info and Details. When Details tab is clicked, I want to pass the title editText to next activity.
P/S : Without button clicked
MainActivity
tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabReselected(p0: TabLayout.Tab?) {
}
override fun onTabSelected(p0: TabLayout.Tab?) {
val position = p0?.position
when (position) {
0 ->
supportFragmentManager.beginTransaction().replace(
R.id.frame_container,
CreateWOInfoFragment()
).addToBackStack(null).commit()
else -> supportFragmentManager.beginTransaction().replace(
R.id.frame_container,
CreateWODetailsFragment()
).addToBackStack(null).commit()
}
}
override fun onTabUnselected(p0: TabLayout.Tab?) {
}
})
FragmentOne would be sending the data entered in EditText to FragmentTwo.
So add an interface in fragment one and let the activity implement this interface,
in Fragment one call the method to send data and in main fragment will handle it to send it to fragment two.
Please check this example Android Passing Data between Fragments

How to setCancelable(false) for standard dialog PreferenceFragmentCompat

I'm using PreferenceFragmentCompat to display and set SharedPreferences. This all works fine. However, I keep getting "W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed." in my logs, because the standard dialog used by PreferencesFragmentCompat does not seem to use the .setCancelable(false) in its showDialog method. I guess I could build my own custom dialog, but that seems a bit of an overkill just to solve this one small problem. Is there any way to simply overwrite the method?
Update:
It was enough to add this to my PreferencesFragmet (removed MultiSelectListPreferenceDialogFragmentCompat, as I don't use it)
#Override
public void onDisplayPreferenceDialog(Preference pref) {
DialogFragment dialogFragment = null;
String DIALOG_FRAGMENT_TAG = "androidx.preference.PreferenceFragment.DIALOG";
if (pref instanceof EditTextPreference) {
dialogFragment = EditTextPreferenceDialogFragmentCompat.newInstance(pref.getKey());
} else if (pref instanceof ListPreference) {
dialogFragment = ListPreferenceDialogFragmentCompat.newInstance(pref.getKey());
}
if (dialogFragment != null) {
dialogFragment.setTargetFragment(this, 0);
dialogFragment.setCancelable(false); //adding this!
if (this.getFragmentManager() != null) {
dialogFragment.show(this.getFragmentManager(), DIALOG_FRAGMENT_TAG);
}
} else {
super.onDisplayPreferenceDialog(pref);
}
}
I sorted though PreferenceFramgnetCompat source code to solve this issue.
Unfortunately, you can't execute '.setCancelable(false)' to dialog without callback or override.
I'll explain it with callback.
You should implement 'PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback' interface on activity which contains PreferenceFragmentCompat fragment.
When user press a preference one of EditTextPreference, ListPreference or AbstractMultiSelectListPreference, the onPreferenceDisplayDialog method will be executed.
When onPreferenceDisplayDialog method is executed, you should open dialog.
Fortunately, there are three type dialog and Google provide it by public so you don't need to make a custom dialog for them.
Just create instance of dialog and call setCancelable(false) and show it!
Please refer below codes.
class SettingsActivity : FragmentActivity(), PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback {
private val DIALOG_FRAGMENT_TAG = "android.support.v7.preference.PreferenceFragment.DIALOG"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager.beginTransaction()
.replace(android.R.id.content, SettingsFragment(), "setting_fragment").commit()
}
override fun onPreferenceDisplayDialog(caller: PreferenceFragmentCompat, preference: Preference?): Boolean {
// check if dialog is already showing
if (supportFragmentManager!!.findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {
return true
}
val f: DialogFragment
if (preference is EditTextPreference) {
f = EditTextPreferenceDialogFragmentCompat.newInstance(preference.getKey())
} else if (preference is ListPreference) {
f = ListPreferenceDialogFragmentCompat.newInstance(preference.getKey())
} else if (preference is AbstractMultiSelectListPreference) {
f = MultiSelectListPreferenceDialogFragmentCompat.newInstance(preference.getKey())
} else {
throw IllegalArgumentException("Tried to display dialog for unknown " + "preference type. Did you forget to override onDisplayPreferenceDialog()?")
}
f.setTargetFragment(supportFragmentManager.findFragmentByTag("setting_fragment"), 0)
f.isCancelable = false // !! HERE !!
f.show(supportFragmentManager!!, DIALOG_FRAGMENT_TAG)
return true
}
}

function like onBackPressed() supportActionBar() import error after migrating to androidx

After migrating from android.support to androidx, all the functions like onBackPressed(), setSupportActionBar and other default functions has stopped working.
How to fix this error?
Dagger 2.17 isn't compatible with Androidx. Use 2.16
After changing version perform clean build and if it's still not working then perform Invalidate Caches / Restart from the File menu
Ok. The issue was with dagger version 2.14. Which uses support version for AppCompatActivity instead of androidx. Upgrading to latest version of dagger will solve this problem
We are using both these function will post code and a fancy onBackPressed fun
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_person)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
initViews()
}
Here is the Option Selected code we have a Menu here as well
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item != null) {
when (item.itemId) {
R.id.addNote -> {
val intent = Intent(this, MainActivity::class.java)
intent.putExtra("FROM","N")// ADD NEW NOTE
startActivity(intent)
}
}
// CODE below manages HOME Button
val id = item.itemId
if (id == android.R.id.home) {
val intent = Intent(this, MainActivity::class.java)
intent.putExtra("FROM","N")// ADD NEW NOTE
startActivity(intent)
}
}
return super.onOptionsItemSelected(item)
}
And now that fancy onBackPressed with GLOBAL TOAST
override fun onBackPressed(){
theTOAST()
}
fun theTOAST(){
val toast = Toast.makeText(this#PersonActivity, "USE <-- to go back",Toast.LENGTH_LONG)
val view = toast.view
view.setBackgroundColor(Color.TRANSPARENT)
val text = view.findViewById(android.R.id.message) as TextView
text.setTextColor(Color.BLUE)
text.textSize = (20F)
toast.show()
}
To make the Toast Global you need a var toastSTR = "" at top level
Then put that var in the Toast where the string would go and set the value and call the fun wrom any fun you want a custom toast

Multiple click listeners on buttons

I want to know how to add multiple click events to buttons defined in XML, as previously, in Java, we implemented View.onClickListener interface and did the rest of the work in onClick method.
Example:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.oneButton:
// do your code
break;
case R.id.twoButton:
// do your code
break;
case R.id.threeButton:
// do your code
break;
default:
break;
}
}
I'm making a basic calculator app with the new Kotlin but it seems Kotlin has no such provisions, instead my code looks too long and verbose, as I'm attaching events to all buttons individually.
Can someone tell me how to do the same way in Kotlin? Thanks
For multiple onClickListeners in kotlin (version:1.1.60), the following helped me. Hope it'll be useful to someone else too.
Implement OnClickListener to activity like:
class YourActivity : AppCompatActivity(), View.OnClickListener
set your button in onCreate():
val button = findViewById<Button>(R.id.buttonId)
and assign onclick to the button in onCreate():
button.setOnClickListener { onClick(button) }
and in the override method of onClick() :
override fun onClick(v: View) {
when (v.id) {
R.id.buttonId -> { //your code }
..
..
..
else -> { //your code }
}
}
Yes, in Kotlin you can do it like this:
view.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
when(v?.id) {
R.id.imgBack -> {/* do your code */}
R.id.twoButton -> {/* do your code */}
R.id.threeButton -> {/* do your code */}
else -> {/* do your code */}
}
}
}
You can try this following code:
class Testing:AppCompatActivity(), View.OnClickListener {
private val mButton1:Button
private val mButton2:Button
protected fun onCreate(savedInstanceState:Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_testing)
mButton1 = findViewById(R.id.btn_click) as Button
mButton2 = findViewById(R.id.btn_click1) as Button
mButton1.setOnClickListener(this)
mButton2.setOnClickListener(this)
}
fun onClick(view:View) {
when (view.getId()) {
R.id.btn_click -> {
Toast.makeText(this, "button 1", Toast.LENGTH_SHORT).show()
}
R.id.btn_click1 -> {
Toast.makeText(this, "button 2", Toast.LENGTH_SHORT).show()
}
else -> {}
}
}
}
I hope this is help you.
First of all implement OnClickListener in your Activity, like
class MainActivity : Activity , OnClickListener
then override its implementation like
func onClick(v:View) {
//use when here like
case R.id.youview -> {
// do your work on click of view
}
Don't forgot to set clicklistener on your View.
yourView.setOnClickListener(this)
Or for better understanding go step by step -
Implement OnClickListener in your Activity.
Compiler will ask you to implement overrided methods. Implement those.
Copy paste your java code which you wrote inside onClick method, that can be converted by kotlin itself or write down when conditions.
This code worked for me:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
imgBack.setOnClickListener(this)
twoButton.setOnClickListener(this)
threeButton.setOnClickListener(this)
}
override fun onClick(view:View) {
when (view.id) {
R.id.imgBack -> {
Toast.makeText(this, "imgBack", Toast.LENGTH_SHORT).show()
}
R.id.twoButton -> {
Toast.makeText(this, "twoButton", Toast.LENGTH_SHORT).show()
}
else -> {}
}
}
Don't forget implement View.OnClickListener in your class.
you can create anonymous object from an inteface View.ClickListener and pass it to setOnClickListener function
private val clickListener = View.OnClickListener { p0 ->
when (p0.id) {
....
}
}
... setOnClickListener(clickListener)
Try below like this.
public class MainActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button xyz = (Button) findViewById(R.id.xyz);
xyz.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}}
More detailed information at https://androidacademic.blogspot.in/2016/12/multiple-buttons-onclicklistener-android.html

Categories

Resources