RobT
RobT

Reputation: 371

Android- Getting the Fragment instance in Activity with Multiple Tabs

I am having problems referencing an already instantiated Fragment from within my Activity.

I have two tabs as part of the Action Bar and for each Tab I am instantiating an instance of the same fragment....

Fragment = Player

Tab1 = Player1 (Player Fragment with details from DB of Player 1) Tab2 = Player2 (Player Fragment with details from DB of Player 2)

The Player fragment includes a field (Games played) which is amendable using a Number Picker. So... I have created an interface which I have implemented on my activity. When the button is clicked on the Fragment it calls the interface which on the activity creates a Dialog Fragment to display the number picker. Once the number picker has been closed and passed back the number of Games Played to the Activity I then want to update this value on the correct Fragment (So if the button was pressed on Tab 2 then the value on the Player2 should get updated.)

This is where I am drawing a blank.

I am using actionBar.addTab to add each tab and using a TabsListener Class implementing ActionBar.TabListener to do a replace of the correct Fragment when each tab is pressed. ActionBar handles the FragmentManager stuff for you... What I think I need to do here within my Activity is get the Current Fragment so that I can make a call to a method in this fragment to update it. But when Adding the Fragment through the TabListener I cannot see a way that I can either get and store the ID of the Fragment instance or set a Tag for it. If I could then I could use getFragmentByID or getFragmentByTag to find it.

Any ideas on how I should do this.


I thought I had a completely different solution whereby I made by database update in the Number Picker itself and then simply let onResume() update the value in my Visible Fragment when the DialogFragment closes but it seems that onResume() is not called when the DialogFragment is closed.

I have not posted code examples as I hope the above simplifies the question.

Upvotes: 0

Views: 824

Answers (2)

NigelK
NigelK

Reputation: 8490

Both the 'add' and 'replace' methods of FragmentTransaction have a number of overloads. Using the one with the parameters:

 (int containerViewId, Fragment fragment, String tag)

...allows you to provide a tag name for the fragment that you can subsequently use to retrieve it with a call to findFragmentByTag.

Upvotes: 1

Manveer Chawla
Manveer Chawla

Reputation: 434

You can specify the TabListener (http://developer.android.com/reference/android/app/ActionBar.TabListener.html) and get notified when tab is reselected and here you can update the UI.

In my opinion you should update the database after user selects the value from the dialog. You can specify OnClickListener for different buttons on dialog and get notified when something is selected or cancelled.

Example at http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog,

public static class MyAlertDialogFragment extends DialogFragment {

  public static MyAlertDialogFragment newInstance(int title) {
      MyAlertDialogFragment frag = new MyAlertDialogFragment();
      Bundle args = new Bundle();
      args.putInt("title", title);
      frag.setArguments(args);
      return frag;
  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
      int title = getArguments().getInt("title");

      return new AlertDialog.Builder(getActivity())
            .setIcon(R.drawable.alert_dialog_icon)
            .setTitle(title)
            .setPositiveButton(R.string.alert_dialog_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ((FragmentAlertDialog)getActivity()).doPositiveClick();
                    }
                }
            )
            .setNegativeButton(R.string.alert_dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ((FragmentAlertDialog)getActivity()).doNegativeClick();
                    }
                }
            )
            .create();
  }

}

Upvotes: 0

Related Questions