1

In my main activity, i have a framelayout which is used to display fragments now imagine, i have 3 fragment objects :

Fragment1 f1 = new Fragment1();
Fragment2 f2 = new Fragment2();
Fragment3 f3 = new Fragment3();

and i set this fragment using :

fragTrans.replace(android.R.id.content, f1);
fragTrans.addToBackStack(null);
fragTrans.commit();

now at different point of times, the app might have one of the three fragments in the framelayout (number of fragments might vary).

So, at some point if i want to identify which of the fragments if being currently displayed in the framelayout, how can i do that ?

My purpose : the fragment2 has say 2 states, where boolean state can be either true or false, if the state is true, and the back button is pressed i want to do something and set state to false and the if the back is pressed i'll call super.onBackPressed() but if it is set to true and if currently fragment 3 is visible, first i want to go to fragment 2, then change the state to false and then super.onBackPressed()

so that will make it piece of cake if i can identify which fragment is currently visible

Prince Dholakiya
  • 3,255
  • 27
  • 43
Femn Dharamshi
  • 527
  • 1
  • 9
  • 25
  • 1
    refer https://stackoverflow.com/questions/6750069/get-the-current-fragment-object – sasikumar Dec 03 '18 at 09:17
  • Possible duplicate of [get currently displayed fragment](https://stackoverflow.com/questions/9294603/get-currently-displayed-fragment) – ADM Dec 03 '18 at 10:02

2 Answers2

3

Try this

Fragment fragment = (Fragment)supportFragmentManager.findFragmentById(R.id.fragment_container) 
ADM
  • 20,406
  • 11
  • 52
  • 83
Archana Pareta
  • 198
  • 1
  • 9
1

Your Solution is here

1. First of you replace the fragment replace line with this line.

fragTrans.replace(android.R.id.content, f1,f1.getClass.getName());

Here f1.getClass,getName() is key of current Fragment. it Gives you name of fragment which is replace or current replace fragment.

        FragmentManager manager = getSupportFragmentManager();

        Fragment1 fragment1 = (Fragment1) manager.findFragmentByTag(Fragment1.class.getName());

        Fragment2 fragment2 = (Fragment2) manager.findFragmentByTag(Fragment2.class.getName());

        Fragment3 fragment3 = (Fragment3) manager.findFragmentByTag(Fragment3.class.getName());

        if (fragment1 != null) {
            Log.e(TAG, "Current fragment is Fragment1");
        }else if(fragment2 != null) {
            Log.e(TAG, "Current fragment is Fragment2");
        }else if(fragment3 != null) {
            Log.e(TAG, "Current fragment is Fragment3");
        } else {
            Log.e(TAG, "Fragment 1-2-3 is null");
        }

If you do something on first time onBackPress Follow the code below,

@Override
    public void onBackPressed() {
        if (isBackPress) { //Default is false
            super.onBackPressed();
        } else {
            // On First time click do something 
               isBackPress=true;
        }
    }
Prince Dholakiya
  • 3,255
  • 27
  • 43
  • But at any point of the app, none of the fragments will be null right ? – Femn Dharamshi Dec 05 '18 at 10:55
  • You have got a fragment that is currently open which is replaced with the use of the key. – Prince Dholakiya Dec 05 '18 at 10:59
  • Butt at anypoint none of the fragments will be null as i have declared the fragment variables as global in the class SubjectFragment fs; CodeFragment cf; so now how do i do this because at any point if i try both will be non-null – Femn Dharamshi Dec 05 '18 at 10:59
  • You are not understood my code. you have to learn android documentation. – Prince Dholakiya Dec 05 '18 at 11:09
  • Just tell me one thing, Won't it be easier to find which Fragment is currently displayed in the FrameLayout by using manager.findFragmentById(R.id.FrameLayout) ? instead of checking the tags of each ? (Tell me a good reason why should i use findFragmentByTag instead of ByID ?) – Femn Dharamshi Dec 05 '18 at 11:14
  • 1
    Please Read this article : https://stackoverflow.com/a/43520981/7832102 – Prince Dholakiya Dec 05 '18 at 11:40