Android_programmer_camera
Android_programmer_camera

Reputation: 13369

How to implement onBackPressed() in Fragments?

Is there a way in which we can implement onBackPressed() in Android Fragment similar to the way in which we implement in Android Activity?

As the Fragment lifecycle do not have onBackPressed(). Is there any other alternative method to over ride onBackPressed() in Android 3.0 fragments?

Upvotes: 640

Views: 694649

Answers (30)

Hw.Master
Hw.Master

Reputation: 4452

Override onBackPressed in the Activity. All the FragmentTransaction are addToBackStack before commit:

@Override
public void onBackPressed() {

    int count = getSupportFragmentManager().getBackStackEntryCount();

    if (count == 0) {
        super.onBackPressed();
        //additional code
    } else {
        getSupportFragmentManager().popBackStack();
    }

}

Upvotes: 352

Francis
Francis

Reputation: 7114

UPDATE: OnBackPressedDispatcher should be used.

Guide how to use available at developer.android.com/guide/navigation/navigation-custom-back


You can register fragment in activity to handle back press:

interface BackPressRegistrar {
    fun registerHandler(handler: BackPressHandler)
    fun unregisterHandler(handler: BackPressHandler)
}

interface BackPressHandler {
    fun onBackPressed(): Boolean
}

usage:

In Fragment:

private val backPressHandler = object : BackPressHandler {
    override fun onBackPressed(): Boolean {
        showClosingWarning()
        return false
    }
}

override fun onResume() {
    super.onResume()
    (activity as? BackPressRegistrar)?.registerHandler(backPressHandler)
}

override fun onStop() {
    (activity as? BackPressRegistrar)?.unregisterHandler(backPressHandler)
    super.onStop()
}

In Activity:

class MainActivity : AppCompatActivity(), BackPressRegistrar {


    private var registeredHandler: BackPressHandler? = null
    override fun registerHandler(handler: BackPressHandler) { registeredHandler = handler }
    override fun unregisterHandler(handler: BackPressHandler) { registeredHandler = null }

    override fun onBackPressed() {
        if (registeredHandler?.onBackPressed() != false) super.onBackPressed()
    }
}

Upvotes: 7

fazal ur Rehman
fazal ur Rehman

Reputation: 404

In case of kotlin use this onAttach callback

        override fun onAttach(context: Context) {
          super.onAttach(context)
           val callback: OnBackPressedCallback = object : 
                 OnBackPressedCallback(true) {
                 override fun handleOnBackPressed() {
                 // your onbackpressed code 


            }
          }
      requireActivity().onBackPressedDispatcher.addCallback(this, callback)
      }

Upvotes: 0

Amal
Amal

Reputation: 321

if fragment and used NavController simply call

findNavController().popBackStack()

Upvotes: 1

Mahmoud Ayman
Mahmoud Ayman

Reputation: 1334

No One Mentioned how do I call activity?.onBackPressed inside this logic.

so I created an extension that may be useful for you:

fun Fragment.onBackClick(callback: (onBackPressedCallback: OnBackPressedCallback) -> Unit) {
// This callback will only be called when MyFragment is at least Started.

    activity?.onBackPressedDispatcher?.addCallback(viewLifecycleOwner) {
        callback.invoke(this)
    }
}

now the exciting part calling onBackPressed when your logic is done:

sample use in a fragment:

onBackClick { 
        if (shouldBackFromFrag){
            //do your logic when pressed back
        }else{
            it.isEnabled = false //the key to disable the dispatcher
            activity?.onBackPressed()
        }
    }

Upvotes: 1

protanvir993
protanvir993

Reputation: 3109

In kotlin, it's way simplier.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            //
        }
    })
}

Upvotes: 20

Braian Coronel
Braian Coronel

Reputation: 22905

If you need to have the implementation in the Fragment you should use an interface.

On the other hand, if you need to have the implementation in the activity such as eg. perform a simple finish() of the activity do the following:

YourActivity.kt

override fun onBackPressed() {
    Log.d(this.javaClass.simpleName, "onBackPressed() called")

    if (supportFragmentManager.fragments.any { it is YourFragment }) {
        finish()
        return
    }
}

Upvotes: 1

IdoT
IdoT

Reputation: 2871

The solution is simple:

  1. If you have a base fragment class that all fragments extend, then add this code to it's class, otherwise create such a base fragment class
/* 
* called when on back pressed to the current fragment that is returned
*/
public void onBackPressed()
{
    // add code in super class when override
}
  1. In your Activity class, override onBackPressed as follows:
private BaseFragment _currentFragment;

@Override
public void onBackPressed()
{
      super.onBackPressed();
     _currentFragment.onBackPressed();
}
  1. In your Fragment class, add your desired code:
@Override
public void onBackPressed()
{
    setUpTitle();
}

Upvotes: 15

Sanjay Joshi
Sanjay Joshi

Reputation: 2066

New and better approach: Following piece of code in a Fragment will help you to capture the back-press event.

JAVA

@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);

    OnBackPressedCallback callback = new OnBackPressedCallback(true) {
        @Override
        public void handleOnBackPressed() {
            Toast.makeText(mContext, "back pressed", Toast.LENGTH_SHORT).show();
            
            // And when you want to go back based on your condition
            if (yourCondition) {
                this.setEnabled(false);
                requireActivity().onBackPressed();
            }
        }
    };

    requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}

Kotlin

activity?.onBackPressedDispatcher?.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
    override fun handleOnBackPressed() {

    }
})

Upvotes: 34

Md Manna
Md Manna

Reputation: 61

private boolean isMainFragment = true;

@Override
public void onBackPressed() {

    if (isMainFragment){
        finish();
    }else {
        getSupportFragmentManager().popBackStack();
        isMainFragment = true;
    }
}

When open anther fragment then just add

isMainFragment = false;

it's work with me

Upvotes: 0

Sudarshan
Sudarshan

Reputation: 1046

From your fragment, just use

 requireActivity().onBackPressed();

For handling click add binding.actionABack.setOnClickListener(v -> requireActivity().onBackPressed());

Upvotes: 3

Ayush Sth
Ayush Sth

Reputation: 332

 requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) {
        //your code 
    }

Upvotes: 6

Anwar Zahid
Anwar Zahid

Reputation: 257

Here is my answer to handle the fragment on the back button pressed. I have the bottom navigation with five items (A, B, C, D,E) to attach to the activity when clicking on an item I attach the fragment to the activity. let suppose click on item(A) I attach fragment(A) and then if click on item(B) then I attach fragment(B) and so on.

The flow of Click item. A->B->C->D->E

The flow of back button pressed. E->D->C->B->A

 @Override
        public void onBackPressed() {
            int count = getSupportFragmentManager().getBackStackEntryCount();
            if (count == 0) {
                super.onBackPressed();
            } else {
                int index = ((getSupportFragmentManager().getBackStackEntryCount()) -1);
                getSupportFragmentManager().popBackStack();
                FragmentManager.BackStackEntry backEntry = getSupportFragmentManager().getBackStackEntryAt(index);
                int stackId = backEntry.getId();
                bottomNavigationView.getMenu().getItem(stackId).setChecked(true);
            }
        }

Here the demo is

Upvotes: 0

Mohd Asim
Mohd Asim

Reputation: 754

Try this one, if you really want to enable onBackPressed() in Fragment. After wasting an hour of time, I made this solution which exactly fits over the needs, from my prior experiences.

You just need to focus on the value of private int STATUS_FRAGMENT=0; which suffices the need of addToBackStack() in fragments.

import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.growfast.NavigationItemsFolder.CoreFragments.Cart;
import com.example.growfast.NavigationItemsFolder.CoreFragments.HelpDesk;
import com.example.growfast.NavigationItemsFolder.CoreFragments.Home;
import com.example.growfast.NavigationItemsFolder.CoreFragments.ProfileDetails;
import com.example.growfast.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class BusinessManagement extends AppCompatActivity {

    public BottomNavigationView bottomNavigationView;
    private int STATUS_FRAGMENT=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_layout);
        setBottomNavigationMenu();

    }

    private void setBottomNavigationMenu() {
        bottomNavigationView = findViewById(R.id.navigation);
        bottomNavigationView.setVisibility(View.VISIBLE);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

            Fragment fragment = null;

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {

                    case R.id.action_home:
                        fragment = new Home();
                        break;

                    case R.id.action_profile:
                        fragment = new ProfileDetails();
                        break;
                    case R.id.action_cart:
                        fragment = new Cart();
                        break;
                    case R.id.action_favourites_menu:
                        fragment = new HelpDesk();
                        break;

                }
                return loadFromFragment(fragment);

            }
        });
        bottomNavigationView.setSelectedItemId(R.id.action_home);
    }

    private boolean loadFromFragment(Fragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.my_container, fragment)
                    .commit();
            STATUS_FRAGMENT=1;
            return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        if (STATUS_FRAGMENT==1) {
            bottomNavigationView.setSelectedItemId(R.id.action_home);
            STATUS_FRAGMENT=0;
            bottomNavigationView.setVisibility(View.VISIBLE);
        }
        else{
            super.onBackPressed();
        }
    }


}```

Upvotes: 1

Amjad Alwareh
Amjad Alwareh

Reputation: 3291

Google has released a new API to deal with onBackPressed in Fragment:

activity?.onBackPressedDispatcher?.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {

            }
        })

Upvotes: 56

Mahesh Pandit
Mahesh Pandit

Reputation: 358

In your mainActivity implement interface for callback

protected mainActivity.OnBackPressedListener onBackPressedListener;

public interface OnBackPressedListener {
    void doBack();
}

public void setOnBackPressedListener(mainActivity.OnBackPressedListener onBackPressedListener) {
    this.onBackPressedListener = onBackPressedListener;
}

@Override
public void onBackPressed() {
    if (onBackPressedListener != null) {
        onBackPressedListener.doBack();
    } else { 
        super.onBackPressed();
    }
}

on fragment implement intefrace OnBackPressedListener which we write in mainActivity like

implements mainActivity.OnBackPressedListener

mainActivity is my base activity write following code in your fragment onCreateView method

((mainActivity) getActivity()).setOnBackPressedListener(this);

and implement OnBackPressedListener interface method doBack

@Override
public void doBack() {
    //call base fragment 
}

now call fragment which you want to call on back pressed using doBack() method

Upvotes: 0

Khamidjon Khamidov
Khamidjon Khamidov

Reputation: 9039

You can use onBackPressedDispatcher of parent activity like this:

val backpress = requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, true) {
            // here dispatcher works for any action when back pressed
}

you can also enable/disable backpress button from fragment any time like this:

backpress.isEnabled = true/false

Upvotes: 6

Maxime Jallu
Maxime Jallu

Reputation: 2382

In my opinion the best solution is:

JAVA SOLUTION

Create simple interface :

public interface IOnBackPressed {
    /**
     * If you return true the back press will not be taken into account, otherwise the activity will act naturally
     * @return true if your processing has priority if not false
     */
    boolean onBackPressed();
}

And in your Activity

public class MyActivity extends Activity {
    @Override public void onBackPressed() {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_container);
       if (!(fragment instanceof IOnBackPressed) || !((IOnBackPressed) fragment).onBackPressed()) {
          super.onBackPressed();
       }
    } ...
}

Finally in your Fragment:

public class MyFragment extends Fragment implements IOnBackPressed{
   @Override
   public boolean onBackPressed() {
       if (myCondition) {
            //action not popBackStack
            return true; 
        } else {
            return false;
        }
    }
}

KOTLIN SOLUTION

1 - Create Interface

interface IOnBackPressed {
    fun onBackPressed(): Boolean
}

2 - Prepare your Activity

class MyActivity : AppCompatActivity() {
    override fun onBackPressed() {
        val fragment =
            this.supportFragmentManager.findFragmentById(R.id.main_container)
        (fragment as? IOnBackPressed)?.onBackPressed()?.not()?.let {
            super.onBackPressed()
        }
    }
}

3 - Implement in your target Fragment

class MyFragment : Fragment(), IOnBackPressed {
    override fun onBackPressed(): Boolean {
        return if (myCondition) {
            //action not popBackStack
            true
        } else {
            false
        }
    }
}

Upvotes: 224

David Elmasllari
David Elmasllari

Reputation: 189

Inside the fragment's onCreate method add the following:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    OnBackPressedCallback callback = new OnBackPressedCallback(true) {
        @Override
        public void handleOnBackPressed() {
            //Handle the back pressed
        }
    };
    requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}

Upvotes: 11

B-GangsteR
B-GangsteR

Reputation: 2704

Using Navigation component you can do it like this:

Java

public class MyFragment extends Fragment {

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This callback will only be called when MyFragment is at least Started.
    OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
        @Override
        public void handleOnBackPressed() {
            // Handle the back button event
        }
    });
    requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);

    // The callback can be enabled or disabled here or in handleOnBackPressed()
}
...
}

Kotlin

class MyFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // This callback will only be called when MyFragment is at least Started.
    val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
        // Handle the back button event
    }

    // The callback can be enabled or disabled here or in the lambda
}
...
}

Upvotes: 28

Mete
Mete

Reputation: 2884

In my solution (Kotlin);

I'm using onBackAlternative function as a parameter on BaseActivity.

BaseActivity

abstract class BaseActivity {

    var onBackPressAlternative: (() -> Unit)? = null

    override fun onBackPressed() {
        if (onBackPressAlternative != null) {
            onBackPressAlternative!!()
        } else {
            super.onBackPressed()
        }
    }
}

I have a function for set onBackPressAlternative on BaseFragment.

BaseFragment

abstract class BaseFragment {

     override fun onStart() {
        super.onStart()
        ...
        setOnBackPressed(null) // Add this
     }

      //Method must be declared as open, for overriding in child class
     open fun setOnBackPressed(onBackAlternative: (() -> Unit)?) {
         (activity as BaseActivity<*, *>).onBackPressAlternative = onBackAlternative
     }
}

Then my onBackPressAlternative is ready to use on fragments.

Sub Fragments

override fun setOnBackPressed(onBackAlternative: (() -> Unit)?) {
    (activity as BaseActivity<*, *>).onBackPressAlternative = {
        // TODO Your own custom onback function 
    }
}

Upvotes: 4

aaronmarino
aaronmarino

Reputation: 3992

If you're using androidx.appcompat:appcompat:1.1.0 or above then you can add an OnBackPressedCallback to your fragment as follows

requireActivity()
    .onBackPressedDispatcher
    .addCallback(this, object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            Log.d(TAG, "Fragment back pressed invoked")
            // Do custom work here    

            // if you want onBackPressed() to be called as normal afterwards
            if (isEnabled) {
                isEnabled = false
                requireActivity().onBackPressed()
            }
        }
    }
)

See https://developer.android.com/guide/navigation/navigation-custom-back

Upvotes: 216

krishnakumarcn
krishnakumarcn

Reputation: 4199

Providing custom back navigation by handling onBackPressed is now more easy with callbacks inside the fragment.

class MyFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val onBackPressedCallback = object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                if (true == conditionForCustomAction) {
                    myCustomActionHere()
                } else  NavHostFragment.findNavController(this@MyFragment).navigateUp();    
        }
    }
    requireActivity().onBackPressedDispatcher.addCallback(
        this, onBackPressedCallback
    )
    ...
}

If you want the default back action based on some condition, you can use:

 NavHostFragment.findNavController(this@MyFragment).navigateUp();

Upvotes: 5

Tasnuva Tavasum oshin
Tasnuva Tavasum oshin

Reputation: 4750

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
    {
       if ((keyCode == KeyEvent.KEYCODE_BACK))
  {
           finish();
    }
       return super.onKeyDown(keyCode, event);
    }

Just Comment Any Key down related method now addToBackStack will work . thanks

Upvotes: -1

joelvarma
joelvarma

Reputation: 160

public class MyActivity extends Activity {

    protected OnBackPressedListener onBackPressedListener;

    public interface OnBackPressedListener {
        void doBack();
    }

    public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
        this.onBackPressedListener = onBackPressedListener;
    }

    @Override
    public void onBackPressed() {
        if (onBackPressedListener != null)
            onBackPressedListener.doBack();
        else
            super.onBackPressed();
    } 

    @Override
    protected void onDestroy() {
        onBackPressedListener = null;
        super.onDestroy();
    }
}

in your fragment add the following, dont forget to implement mainactivity's interface.

public class MyFragment extends Framgent implements MyActivity.OnBackPressedListener {
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
         ((MyActivity) getActivity()).setOnBackPressedListener(this);
    }

@Override
public void doBack() {
    //BackPressed in activity will call this;
}

}

Upvotes: 7

TonnyL
TonnyL

Reputation: 1345

According to the AndroidX release notes, androidx.activity 1.0.0-alpha01 is released and introduces ComponentActivity, a new base class of the existing FragmentActivity and AppCompatActivity. And this release brings us a new feature:

You can now register an OnBackPressedCallback via addOnBackPressedCallback to receive onBackPressed() callbacks without needing to override the method in your activity.

Upvotes: 5

b0b
b0b

Reputation: 464

Simply do it in onKeyUp() :

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // do something
        return true;  // return true if back handled, false otherwise
    }

    return super.onKeyUp(keyCode, event);
}

Upvotes: 1

Haider Malik
Haider Malik

Reputation: 1771

Ok guys I finally found out a good solution.

In your onCreate() in your activity housing your fragments add a backstack change listener like so:

    fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            List<Fragment> f = fragmentManager.getFragments();
            //List<Fragment> f only returns one value
            Fragment frag = f.get(0);
            currentFragment = frag.getClass().getSimpleName();
        }
    });

(Also adding my fragmenManager is declared in the activities O Now every time you change fragment the current fragment String will become the name of the current fragment. Then in the activities onBackPressed() you can control the actions of your back button as so:

    @Override
    public void onBackPressed() {

    switch (currentFragment) {
        case "FragmentOne":
            // your code here
            return;
        case "FragmentTwo":
            // your code here
            return;
        default:
            fragmentManager.popBackStack();
            // default action for any other fragment (return to previous)
    }

}

I can confirm that this method works for me.

Upvotes: 1

yasin badur
yasin badur

Reputation: 1

public interface IonBackPressInFrag {
    void backPressed();
}

public class FragmentMainActivity extends AppCompatActivity {
    public IonBackPressInFrag backPressInFrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        @Override
        public void onBackPressed () {
            backPressInFrag.backPressed();
        }
    }
}

public class FragDetailSearch extends Fragment implements IonBackPressInFrag {
 if(getActivity() !=null){
        ((FragmentMainActivity) getActivity()).backPressInFrag = this;
    }

    @Override
    public void backPressed() {
        Toast.makeText(getContext(), "backkkkkk", Toast.LENGTH_LONG).show();
    }
}

Upvotes: -1

user3709410
user3709410

Reputation: 51

This line of code will do the trick from within any fragment, it will pop the current fragment on the backstack.

getActivity().getSupportFragmentManager().popBackStack();

Upvotes: -1

Related Questions