kiduxa
kiduxa

Reputation: 3351

how to know which fragment called another fragment?

I have this issue in Android. Consider three Fragments: A, B, C. C can be called from A or from B. Is there anyway to know from which fragment, C was called?

Edited

Ok guys I'm going to explain what I'm trying to do. Suppose I have this call: A calls B, B calls C.

When I press the back button in C It gets me to B, thats fine. But when I press the back button again, it takes me to C, instead of A.

This is my code:

@Override
public void onBackPressed() { 

    //this is the current fragment
    Fragment fragmentActual =     this.getSupportFragmentManager().findFragmentById(android.R.id.tabcontent);

    String fragmentTag = fragmentoActual.getTag().toString();

            //If I press the back button in C:
    if(fragmentTag.equals("TAG C")){

        Fragment removefragment = this.getSupportFragmentManager().findFragmentByTag("TAG B");
        Fragment fragmentClient = this.getSupportFragmentManager().findFragmentByTag("TAG C");

        //If Im NOT passing arguments to B, I know this is a new form
        if(removefragment.getArguments()== null){

                            //I always pass arguments to fragment C, but just in case:
            if(fragmentClient.getArguments()!= null){

                Bundle mArguments = fragmentClient.getArguments();

                                    //FRAGMENT B
                FragmentB  fragmentB = new FragmentB ();
                fragment.setArguments(mArguments);

                FragmentManager manager = this.getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();


                ft.addToBackStack(null);
                    ft.replace(android.R.id.tabcontent,fragmentB,"TAG B");
                    ft.commit();
                }
        }else{

            super.onBackPressed();
        }

    }else{
        super.onBackPressed();
    }

}   

Now I'm going to explain the code. Basically what it does is to replace fragment B, when fragment C is called. I do this, because I need to pass arguments to fragment B. But when I do the replacement, the "history" of the fragment B is lost, I mean when I press back button in B, I cant go back to fragment A (HERE IS WHY I WANTED TO KNOW IF I CAN KNOW WHO CALLS WHOM).

The firts time when I call fragment B, I dont pass arguments because is a blank form. But when I call to C, staying in B, I need to pass arguments to fragment B (when back button is pressed), so It can shows updated information.

Please if there something that is not clear, let me know so I can explain myself better.

Edited 2: This issue has something with this https://stackoverflow.com/questions/16703604/back-press-button-when-i-save-a-form-for-the-first-time-a-list-view-is-not-updat. Maybe it does my idea more clear.

Upvotes: 1

Views: 1856

Answers (4)

kiduxa
kiduxa

Reputation: 3351

The answer of Luksprog I think is the best: "You may want to tackle those issues. You always have the option of passing a Bundle with data to the newly created fragment mentioning who called it. Then using getArgument() in the fragment will know who called it.".

I haven't found another better way.

Upvotes: 3

Urizev
Urizev

Reputation: 561

You can use the setTargetFragment method to set which was the parent fragment. Then you can use the method getTargetFragment to find out who called you.

Upvotes: 1

DuKes0mE
DuKes0mE

Reputation: 1131

You can use FragmentManager for creating a back stack of your fragments. You also have to work with Fragment Transactions first. Further informations see: http://developer.android.com/guide/components/fragments.html#Transactions

Upvotes: 0

user934357
user934357

Reputation:

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

i guess, This is what you need.

private final static String TAG_FRAGMENT = "TAG_FRAGMENT";

private void showFragment() {
    final Myfragment fragment = new MyFragment();
    final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
    transaction.addToBackStack(null);
    transaction.commit();
}

@Override
public void onBackPressed() {
    final Myfragment fragment = (Myfragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);

    if (fragment.allowBackPressed()) { // and then you define a method allowBackPressed with the logic to allow back pressed or not
        super.onBackPressed();
    }
}

Upvotes: 0

Related Questions