1

Hello I'm developing an android app using 3 Fragments (Fragment A, B, C) inside viewpager and tabs, the viewpager works fine. The fragment A contains a List View, when the user clicks a item, the app open a Fragment Dialog with information about the item selected. This dialog has a button called "Add to favorites". Now I want to do this when user press button:

  1. close the fragment dialog
  2. show the fragment B inside the view pager
  3. send the information from dialog fragment to fragment B

How can I do this?

This is part of my code:

* MainFragmentActivity * (This works fine)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tube);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });


    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {

        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
}

@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            FragmentA a = new FragmentA();
            Bundle args1 = new Bundle();
            args1.putInt(FragmentA.ARG_SECTION_NAME , position + 1);
            a.setArguments(args1);
            return a;

        case 1:
            FragmentB b= new FragmentB();
            Bundle args2 = new Bundle();
            args2.putInt(FragmentB.ARG_SECTION_NAME , position + 2);
            b.setArguments(args2);
            return b;

        case 2:
            FragmentC c= new FragmentC();
            Bundle args3 = new Bundle();
            args3.putInt(FragmentC.ARG_SECTION_NAME , position + 3);
            c.setArguments(args3);
            return c;

          default:
              return null;
        }
    }

This is the Fragment Dialog

* FragmentDialogView *

public class FragmentDialogView extends DialogFragment implements OnClickListener {

private static final int REAUTH_ACTIVITY_CODE = 0;
private String videoId;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    Bundle mArgs = getArguments();

    View view = (View) inflater.inflate(R.layout.fragment_dialog_view, container, false);

    //Buttons
    Button button = (Button) view.findViewById(R.id.button_one);
    button.setOnClickListener(this);
    buttonDownload.setOnClickListener(this);

    return view;
}

@Override
public void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REAUTH_ACTIVITY_CODE) {

    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_one:

            //Here it should show the fragment B inside the viewpager

        break;
    default:                
        break;
   }

}
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
deleon
  • 63
  • 4

2 Answers2

3

To dismiss the Dialog include the following in your DialogFragment's class

private Dialog dialog;

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        dialog = new Dialog(getActivity());
        return dialog;
    }

    @Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_one:

            dismiss();

        break;
    default:                
        break;
   }

}

And create a interface

Create the following Communicator.java

public interface Communicator {
    public void respond(int i);
}

Implement this Communicator in your MainAcitvity

And create a instance of this Communicator in your fragment like this

public class FragmentDialogView extends DialogFragment implements OnClickListener {

    private Communicator com;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        com = (Communicator) getActivity();
        btn.setOnClickListener(this);
        }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.btn:
            com.respond(1);
            break;
        }
    }

Whenever you click that button it sends the int to the method which is residing inside the MainActivity

which will look like following

@Override
public void respond(int i) {


        // Receive a bundle here
        // and pass the corresponding information to the FragmentB
        // here i'm receving an int and pass it to the FragmentB as a String

        FragmentManager fm = getFragmentManager();
        FragmentB fragment = (FragmentB) fm.findFragmentByTag("FragmentB");
        fragment.fromMainActivity(""+i);

        // If the above the one doesn't work keep the instance as Static and then try

        viewPager.invalidate();
        pagerAdapter.notifyDataSetChanged();
        viewPager.setCurrentItem(1, true);

    // Inside the setCuttentItem() method 0 first tab
    // 1 second tab
    // 2 third tab and so on
    } 

Here I'm receiving an int . You can use a bundle to pass the corresponding information. This will change the viewPager to show the next tab as well

and keep any simple method insdie the FragmentB like the following

public void fromMainActivity(String sample) {
    Toast.makeText(getActivity(), sample, duration).show();
}

I hope this would help :) Happy coding

Akbarsha
  • 2,422
  • 23
  • 34
  • Thanks Sha works fine, If try to pass information i need create another instance to FragmentB? If i create a Bundle at OnActivityCreated this will get Null because when the app start the bundle for this fragment doesn't have any value – deleon Apr 02 '14 at 06:35
  • Yes. you need to create a instance if it doesn't work, try make it as a `static`. you're going to create the bundle from the `DialogFrament` rite. so it won't be null if you're passing anything. otherwise check a condition there for null @deleon – Akbarsha Apr 02 '14 at 06:47
2

1.Try this : getDialog().dismiss();

2.As I understood correctly, create a method like this in your fragment ,

public static FirstFragment newInstance(String text){
    FirstFragment f= new FirstFragment();
    return f;
}

Call it in your button onClick() such as FirstFragment.newInstance("Fragment, Instance 1");

3.Create Interface with the method in your DialogFragment can call to pass any data you want back to Fragment that created said DialogFragment. Also set your Fragment as target such as myFragmentDialog.setTargetFragment(this, 0). Then in dialog, get your target fragment object with getTargetFragment() and cast to interface you created. Now you can pass the data using ((MyInterface)getTargetFragment()).methodToPassData(data). For more info : link

Community
  • 1
  • 1
Zusee Weekin
  • 1,348
  • 2
  • 18
  • 41
  • I did that... look my first class. i dont have problems with the fragments in the view pager, my question is how can I show the position B (Fragment) when the user press the button inside Dialog Fragment – deleon Apr 02 '14 at 04:49
  • Did you try the suggested solution, calling the given method inside your button onClick() ? – Zusee Weekin Apr 02 '14 at 04:56
  • I have use this for similar kind of scenario. What I mean here is, create a new instance of your fragment B when you press the button. Did you get any error? – Zusee Weekin Apr 02 '14 at 06:04