Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment: which callback invoked when press back button & customize it

I have a fragment:

public class MyFragment extends Fragment{
     ...
     @Override
     public View onCreateView(...){...}    
     ...
}

I instantiate it:

MyFragment myFragment = new MyFragment();

I use the above fragment to replace the current fragment:

FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

// replace fragment
fragmentTransaction.replace(R.id.fragment_placeholder, myFragment, "myTag");

// NOTE: I did not add to back stack

Now, myFragment is showing on the screen. NOTE: I did not add myFragment to back stack.

My two questions:

1. If now, I press mobile phone back button, which fragment's life cycle callback will be invoked??

2. How can I customize the back button click listener in MyFragment class? (please do not suggest me to do myFragment.getView().setOnclickListener, but do it in MyFragment class)

like image 528
Leem.fin Avatar asked Mar 14 '12 14:03

Leem.fin


People also ask

What happens when back button is pressed in Android?

Once you press the back key the activity's onDestroy() method will be called and the activity will be flushed out of the memory. You will then be required to restart the activity by calling the startActivity() method which will in turn call its onCreate() Method.

What is onAttach in fragment?

onAttach(Activity) called once the fragment is associated with its activity. onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.

How can I tell if my Android back button is pressed?

In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so.


2 Answers

Question 1: See http://developer.android.com/reference/android/app/Fragment.html#Lifecycle:

"As a fragment is no longer being used, it goes through a reverse series of callbacks:

onPause() - fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.

onStop() - fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.

onDestroyView() - allows the fragment to clean up resources associated with its View.

onDestroy() - called to do final cleanup of the fragment's state.

onDetach() - called immediately prior to the fragment no longer being associated with its activity."

Question 2: If you must know that it was the back button specifically that is triggering the callbacks, You can capture the back button press in your Fragment's Activity and use your own method to handle it:

public class MyActivity extends Activity
{
    //...
    //Defined in Activity class, so override
    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        myFragment.onBackPressed();
    }
}

public class MyFragment extends Fragment
{
    //Your created method
    public void onBackPressed()
    {
        //Handle any cleanup you don't always want done in the normal lifecycle
    }
}
like image 66
Taylor Clark Avatar answered Oct 25 '22 03:10

Taylor Clark


androidx.activity 1.0.0-alpha01 is released and introduces ComponentActivity, a new base class of the existing FragmentActivity and AppCompatActivity.

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

like image 24
Darish Avatar answered Oct 25 '22 03:10

Darish