Android back button close popup if opened - java

I have a main activity , and a popup.xml file that is included in the activity
the problem is when i press the back button , it closes the app directly , whether the popup is opened or not
i got the idea to override the onClick method, add a boolean that will be true when the popup is opened , and false otherwise , then add this condition in the onClick method
i'm still a noob with Android Studio , would anyone please guide me through ?
Thank you.

override the onBackPressed in your activity and check if popup is showing. if popup is showing then close popup else do general back press action
#Override
public void onBackPressed() {
if(popupWindow.isShowing())
popupWindow.dismiss();
else
super.onBackPressed();
}

Just override the following method in your Activity:
#Override
public void onBackPressed()
{
//Do whatever you want before the back button should trigger
super.onBackPressed(); // call this only if you want to close the app
}

#Override
public void onBackPressed() {
if(!(Activity).isFinishing){
//activity is not yet finished
}else{
//activity finishes
super.onBackPressed();
}
}

Related

How to override a method when a button is tapped?

I was thinking of creating a button that when tapped can disable the phone back button. I wanted to be able to enable it back again by pressing another button. However, the way that I found to disable the back button was with an override. Could somebody lend me a hand on how I could do that? Thanks!
What I tried was to put the override inside the button listener and onClick method, but it highland the override in red. I then tried putting the override in a different class and then calling the class when the the button is tapped.
I figured it out, but for anyone wondering the same thing, I'll post what I did. I implemented the following code inside MainActivity.class with the buttons:
private boolean backButtonEnabled = true;
#Override
public void onBackPressed() {
if (backButtonEnabled) {
super.onBackPressed();
}
}
public void disableBackButton() {
backButtonEnabled = false;
}
public void enableBackButton() {
backButtonEnabled = true;
}

Press back button on main activity opens other activity

In my project i move from main activity to activity A and from activity A to activity B. From activity B using home menu on toolbar i jump back to main activity. Now when i press back button application should exit but it opens the activity A again.
You should make use of the launch flags to manage your activities in the back stack. As far as I understood your scenario, I think you need to use FLAG_ACTIVITY_CLEAR_TOP for starting your main/home activity.
Read more about launch flags: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP.
Also, take a look this for more details on managing activity back stack on Android: https://developer.android.com/guide/components/activities/tasks-and-back-stack#ManagingTasks
Call the finish() method before starting the next activity to have it removed from the activity. Find more details and options here.
For Kotlin write this in your MainActivity :
override fun onBackPressed() {
moveTaskToBack(true)
exitProcess(-1)
}
For Java write this in your MainActivity :
#Override
void onBackPressed() {
moveTaskToBack(true)
exitProcess(-1)
}
Hope to it will work for you as good as for me
hey i write some code for you
Variables:
boolean backactivity = true;
CODE:
public boolean onOptionsItemSelected(MenuItem item){
if(backactivity==true)
{
finishActivity(1);
backactivity=false;
}else
{
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return true;
}

Prevent ActionBar back button to recreate the MainActivity

How can I prevent the ActionBar back button (we gonna say ABBB) of my SecondActivity to recreate the MainActivitywhen clicked ?
I have a ListView in the MainActivity and it can be edited using the SecondActivity. The problem is that when the user presses the ABBB, the ListView is reset to default...
However, when the user clicks my OK button or presses physical back button, there is not any problem because I'm using finish();
#Override
public void onBackPressed() {
finish();
}
If I use this code... :
switch (item.getItemId()) {
case (android.R.id.home):
finish();
}
...there is the same problem because, I guess, this method is called after "Android's code".
How can I totally override Android ABBB's clicked code to set it to just call finish(); ?
The ideal scenario is, when are you in the SecondActivity (I take it that, this means that you are in Edit mode), and you press the device back button or ABBB, you show a subtle alert to the user saying "do they really want to dismiss the editing", or go ahead and apply the edit done as in the Contacts application.
So that being said, if all you require is to finish() the activity on press of ABBB, the code that you shown above should work fine (though you need to put return true; after finish()). It works for me. Try the below one in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed(); // or finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I put onBackPressed(); because your ABBB with now imitate your device back button.
Or, you can set your parent activity's launch mode as -
android:launchMode="singleTop"
that will do all without changing anything.
Reference

What is the equivalent of onBackPressed() for overriding and taking control of the Recent Apps button on Android?

This is the method to access Android's Back button.
#Override
public void onBackPressed() {
// do something on back.
return;
}
I want to do something similar for the Recent Apps button (the right most one)
You can really change the functionality of the AppButton programatically except if you are creating your own custom launcher but you can still detect if the AppButton is pressed in your app.
sample:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(!hasFocus)
Log.d("APP BUTTON", "APP BUTTON");
}

Android View onResume method?

When returning to an activity using the back button and
startActivity(new Intent(this, ActivityMainMenu.class));
is called, are there any methods that automatically go to a custom view?
I've noticed that when going back to the view, it's no longer invalidated.
Basically without using the activity's onResume I want to be able to resume my custom view.
For anyone else who wants to know you can use:
protected void onAttachedToWindow()
It's called every time a view is attached to the Window.
In the Android Source for TextView, it posts a Runnable.
if (ss.error != null) {
final CharSequence error = ss.error;
// Display the error later, after the first layout pass
post(new Runnable() {
public void run() {
setError(error);
}
});
}

Categories