Asked  7 Months ago    Answers:  5   Viewed   42 times

I have some fragments in my activity

[1], [2], [3], [4], [5], [6]

And on Back Button Press I must to return from [2] to [1] if current active fragment is [2], or do nothing otherwise.

What is the best practise to do that?

EDIT: Application must not return to [2] from [3]...[6]



When you are transitioning between Fragments, call addToBackStack() as part of your FragmentTransaction:

FragmentTransaction tx = fragmentManager.beginTransation();
tx.replace(, new MyFragment() ).addToBackStack( "tag" ).commit();

If you require more detailed control (i.e. when some Fragments are visible, you want to suppress the back key) you can set an OnKeyListener on the parent view of your fragment:

//You need to add the following line for this solution to work; thanks skayred
fragment.getView().setOnKeyListener( new OnKeyListener()
    public boolean onKey( View v, int keyCode, KeyEvent event )
        if( keyCode == KeyEvent.KEYCODE_BACK )
            return true;
        return false;
} );
Tuesday, June 1, 2021
answered 7 Months ago

Make public a method in fragment which will clear your list and call that method from its parent activity's On onBackPressed using fragment's instance.

Friday, August 27, 2021
answered 4 Months ago

Recently the is deprecated

It is not deprecated at the present time. For example, it is not marked as deprecated in the documentation.

'FragmentStatePagerAdapter(' is deprecated

The single-parameter FragmentStatePagerAdapter constructor is deprecated. However, if you read the documentation for that constructor, you will find:

This constructor is deprecated. use FragmentStatePagerAdapter(FragmentManager, int) with BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT

So, replace FragmentStatePagerAdapter(fm) with FragmentStatePagerAdapter(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT), to retain the functionality from the original one-parameter constructor.

Wednesday, October 6, 2021
Deepanshu Goyal
answered 2 Months ago
public static final int SERVICES_ALL = 1;
public static final int SERVICES_FEATURED = 2;
public static final int SERVICES_NEW = 3;

should be

public static final int SERVICES_ALL = 0;
public static final int SERVICES_FEATURED = 1;
public static final int SERVICES_NEW = 2;

getItem is 0-based-index, which means that in your code, the case 0 of your Adapter never matches one, and in that case you are returning a null fragment causing the exception

Tuesday, October 12, 2021
answered 2 Months ago

The second approach you tried is correct.Try to call popBackStack() on BackPressed() by overriding onBackPressed() method.

public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
    } else if (getFragmentManager().getBackStackEntryCount() == 0) {
Tuesday, November 9, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :