4

I've done some research but I really couldn't find the answer.

I have single activity with side menu, and holder. I have many (non support) fragments, and all of them are using same holder (one at a time).

When user uses menu (it's in main activity), and goes another page, I want to add name of the current fragment to backstack (using .addToBackStack(Fragment1.class.getName())), but I couldn't find how to get current fragment.

I don't want to implement interface etc to keep track of current fragment. There is a super simple way using fragmentManger isn't there?

Mohamed
  • 1,251
  • 3
  • 15
  • 36

5 Answers5

8

You can get your current fragment like this:

if (getFragmentManager().getBackStackEntryCount() > 1) {
            Fragment f = getFragmentManager().findFragmentById(R.id.content_frame);
            if (f instanceof BlankFragment) {
                // Do something
            }
}
AndroidGeek
  • 399
  • 2
  • 9
6

OK,

If you want to get latest entry from backstack(thanks to @AndroidGeek);

fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-1);

and, if you want to get currently active fragment (thanks to @Salman500 @AndroidGeek);

Fragment f = getFragmentManager().findFragmentById(R.id.fragment_holder);
Mohamed
  • 1,251
  • 3
  • 15
  • 36
1

you can use this to get fragment id for non support fragment

Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_id);

    if(fragment!=null)
    {
        getFragmentManager()
                .beginTransaction()
                .addToBackStack(null)
                .commit();
    }
Salman500
  • 1,213
  • 1
  • 17
  • 35
0

Just use addToBackStack() before you commit() the fragment trancsaction. See it here

So your code will look like :

...
fragmentTransaction.replace(R.id.holder, newFragmentToShow, newFragmentTag);
fragmentTransaction.addToBackStack();
fragmentTransaction.commit();
...

EDIT : after OP was edited

You do not need the fragment class to call addToBackStack() as you have mentioned in the OP. The String argument is an optional string just to keep the state for the backstack state. You should see the documentation

It is all internally managed and the current active fragment is automatically added to the backStack, you may call it from where ever you want, it will always use current active fragment.

zeekhuge
  • 1,594
  • 1
  • 13
  • 23
  • Question is how can I get current fragment (or its class name) which is active, from main activity. – Mohamed Jul 21 '17 at 22:03
  • Main activity is the one that replaces the fragment . isnt it ? so main activity should already know what fragment it is displaying (use one variable, thats it). As for adding to the backtstack, you just need to use `addToBackStack()` before you replace the current fragment with the new one. – zeekhuge Jul 21 '17 at 22:11
  • Menu is not the only place that I replace fragments. It could be changed from inside of fragments. I know about second part. – Mohamed Jul 21 '17 at 22:14
  • Edited the ans. – zeekhuge Jul 21 '17 at 22:28
  • Thanks for suggestion, but as I've said I don't want to keep track of it myself. I'm aware of how it works too. It is stack, and I only want to get the one which is at the top of it, just like .pop() – Mohamed Jul 21 '17 at 22:32
0

You can keep track of fragments in the main activity (with variables) and access them there. Example:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction= manager.beginTransaction();

MyFragment myFragment = new MyFragment();

myFragment.doSomething();

Adding to the back-stack:

FragmentTransaction fragment = getSupportFragmentManager().beginTransaction();
fragment.addToBackStack(fragment);
fragment.commit();

This is answered here: get currently displayed fragment

escapesequence
  • 202
  • 4
  • 12
  • Problem is I don't use support library, so SupportFragmentManager. Can I use it without using support fragment? – Mohamed Jul 21 '17 at 22:06