1

So the problem is not a technical issue, but more of an aesthetic kind.

I am passing data from one fragment to another fragment. So there are 3 fragments in total and the first two are calling the same 3rd fragment, so to identify in the third fragment from which of the first two it was invoked I am using the following code

in the first fragment

Bundle bundle = new Bundle();
bundle.putBoolean("isFirst", true)
Fragment fragment = new ThirdFragment();
fragment.setArguments(bundle);
loadFragment(fragment);

and in the second fragment as

Bundle bundle = new Bundle();
bundle.putBoolean("isFirst", false)
Fragment fragment = new ThirdFragment();
fragment.setArguments(bundle);
loadFragment(fragment);

and in the third fragment

Bundle bundle = this.getArguments();
Boolean isFirst = bundle.getBoolean("isFirst",false);
if(isFirst){
....
} else{
....
}

the code works fine but I think there should be a more elegant way of doing it. If not atleast make the whole thing into one line without additional declarations such as Bundle bundle and Fragment fragment

for example something like

loadFragment(new ThirdFragment().setArguments(new Bundle().putBoolean("isFirst",true)));

I am relatively new to android and java programming so please don't be harsh.

Ishan Dragenox
  • 135
  • 1
  • 1
  • 9

3 Answers3

0

I think you should declare static method getInstance(Boolean isFirst) in ThirdFragment example

public static ThirdFragment getInstance(Boolean isFirst) {
   Bundle bundle = new Bundle();
   bundle.putBoolean("isFirst", isFirst)
   Fragment fragment = new ThirdFragment();
   fragment.setArguments(bundle);
   return fragment
}

In First and SecondFragment call

loadFragment(ThirdFragment.getInstance(true));
Công Hải
  • 4,961
  • 3
  • 14
  • 20
0

you can create newIstance() in ThirdFragment like stated here

you can also achieve the same by the below code

public class Util {

static Fragment getThirdFragment(FragmentManager fragmentManager, Boolean isFirst, @NonNull ClassLoader classLoader, @NonNull String className) {
    Bundle bundle = new Bundle();
    bundle.putBoolean("isFirst", isFirst);
    Fragment fragment = fragmentManager.getFragmentFactory().instantiate(classLoader, className);
    fragment.setArguments(bundle);
    return fragment;
    }
}

and call it

loadFragment(Util.getThirdFragment(getSupportFragmentManager(),true/*is first boolean value*/, ThirdFragment.class.getClassLoader(), ThirdFragment.class.getName()));

Kotlin

put this code inside ThirdFragment

companion object {
    @JvmStatic
    fun newInstance(param1: Boolean) =
            ThirdFragment().apply {
                arguments = Bundle().apply {
                    putBoolean("isFirst", param1)
                }
            }
}
Mohammed Alaa
  • 3,140
  • 2
  • 19
  • 21
0

use bundleOf("key" to "value") ex :

findNavController(it).navigate(
    R.id.action_sendSmsFragment_to_webViewFragment,
    bundleOf(
    "age" to 25,
    "name" to "Michael",
    "skill" to null
     )
 )
The MJ
  • 453
  • 7
  • 17