Reputation: 105
i manage to pass some data from fragment to dialog fragment , i using bundle to implement passing data function. but the problem is i keep get the null value in my dialog fragment even when i debug the program is already showing that my bundle contain the data already. is it i can't use bundle to passing the data or my way to using bundle is wrong.
MainActivity
public class MainActivity extends Fragment {
Button test;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View h = inflater.inflate(R.layout.activity_main, container, false);
test = (Button) h.findViewById(R.id.test);
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle args = new Bundle();
args.putString("key", "asdasdasdasd");
FragmentDialog newFragment = new FragmentDialog();
newFragment.setArguments(args);
newFragment.show(getActivity().getSupportFragmentManager(), "TAG");
}
});
onOpenDialog();
return h;
}
public void onOpenDialog()
{
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentDialog overlay = new FragmentDialog();
overlay.show(fm, "FragmentDialog");
}
}
FragmentDialog
public class FragmentDialog extends DialogFragment
{
private SectionsPagerAdapter sectionsPagerAdapter;
private ViewPager viewPager;
TextView textView;
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState)
{
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dialog, container);
// tab slider
sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
// Set up the ViewPager with the sections adapter.
viewPager = (ViewPager)view.findViewById(R.id.pager);
viewPager.setAdapter(sectionsPagerAdapter);
textView =(TextView)view.findViewById(R.id.textview);
Bundle mArgs = getArguments();
String myValue = mArgs.getString("key");
textView.setText(myValue);
return view;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0)
{
// find first fragment...
Fragment_Tab_1 ft1 = new Fragment_Tab_1();
return ft1;
}
if (position == 1)
{
// find first fragment...
Fragment_Tab_2 ft2 = new Fragment_Tab_2();
return ft2;
}
else if (position == 2)
{
// find first fragment...
Fragment_Tab_3 ft3 = new Fragment_Tab_3();
return ft3;
}
return null;
}
@Override
public int getCount() {
// Show 2 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "First Tab";
case 1:
return "Second Tab";
case 2:
return "Third Tab";
}
return null;
}
}
}
Upvotes: 0
Views: 2516
Reputation:
Try this:
In your MainActivity:
FragmentTransaction transection=getFragmentManager().beginTransaction();
FragmentDialog mfragment=new FragmentDialog ();
//using Bundle to send data
Bundle bundle=new Bundle();
bundle.putString("key","asdasdasdasd");
mfragment.setArguments(bundle); //data being send to SecondFragment
transection.replace(R.id.frag, mfragment);
transection.commit();
In onCreateView of FragmentDialog:
String myStr = getArguments().getString("key");
text.setText(myStr);
Upvotes: 1
Reputation: 41
you can use newInstance static methode:
public static FragmentDialog newInstance(int someInt) {
FragmentDialog yourFragmentDialog = new FragmentDialog();
Bundle args = new Bundle();
args.putInt("intVariable", someInt);
args.putString("stringVariable", someString);
yourFragmentDialog.setArguments(args);
return yourFragmentDialog;
}
and then to get the data:
getArguments().getInt("intVariable", 0);
Hope that it helps :)
Upvotes: 1
Reputation: 2810
Please try this
public void onOpenDialog()
{
Bundle args = new Bundle();
args.putString("key", "KEY_FROM_OPEN_DIALOG");
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentDialog overlay = new FragmentDialog();
overlay.setArguments(args);
overlay.show(fm, "FragmentDialog");
}
Upvotes: 0