Skip to content

Instantly share code, notes, and snippets.

@NoNews
Created January 7, 2019 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NoNews/ef60ff69944efe14f5e28a7439169303 to your computer and use it in GitHub Desktop.
Save NoNews/ef60ff69944efe14f5e28a7439169303 to your computer and use it in GitHub Desktop.
Work with fragments with transfer data
public class Dog implements Serializable {
private String name;
public Dog(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class FragmentA extends Fragment {
private static final int LAYOUT = R.layout.fragment_a;
public static FragmentA newInstance() {
Bundle args = new Bundle();
FragmentA fragment = new FragmentA();
fragment.setArguments(args);
return fragment;
}
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(LAYOUT, container, false);
final Button btnOpenFragmenrB = view.findViewById(R.id.btn_open_fragment_b);
btnOpenFragmenrB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentActivity fragmentActivity = getActivity();
if (fragmentActivity == null) {
return;
}
Navigator navigator = (Navigator) fragmentActivity;
Dog dog = new Dog("Yana");
navigator.openBScreen(dog);
}
});
return view;
}
}
public class FragmentB extends Fragment {
private static final String EXTRA_DOG = "dog_key";
private static final int LAYOUT = R.layout.fragment_b;
public static FragmentB newInstance(Dog dog) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_DOG, dog);
FragmentB fragment = new FragmentB();
fragment.setArguments(args);
return fragment;
}
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(LAYOUT, container, false);
final Bundle arguments = getArguments();
final TextView tvDogName = view.findViewById(R.id.tv_dog_name);
if (arguments != null) {
Dog dog = (Dog) arguments.getSerializable(EXTRA_DOG);
if (dog != null) {
String name = dog.getName();
tvDogName.setText(name);
}
}
return view;
}
}
public class MainActivity extends AppCompatActivity implements Navigator{
private static final String FRAGMENT_B_KEY = "Fragment_b_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openFirstScreen();
}
public void openFirstScreen() {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(android.R.id.content, FragmentA.newInstance())
.commit();
}
@Override
public void openBScreen(@NonNull Dog dog) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(android.R.id.content, FragmentB.newInstance(dog))
.addToBackStack(FRAGMENT_B_KEY)
.commit();
}
}
public interface Navigator {
void openBScreen(@NonNull Dog dog);
}
@riyash0109
Copy link

To pass data from an Activity to a Fragment using ViewModel in Java, you can follow these steps:

Step1 : Create a ViewModel class for sharing data between the Activity and the Fragment.

public class MyViewModel extends ViewModel {
private MutableLiveData myData = new MutableLiveData<>();

public void setData(String data) {
    myData.setValue(data);
}

public LiveData<String> getData() {
    return myData;
}

}

Step2 : In the Activity, create an instance of the ViewModel and set the data using the setData() method.

MyViewModel myViewModel = new ViewModelProvider(this).get(MyViewModel.class);
myViewModel.setData("Hello from Activity!");

Step3 : In the Fragment, get an instance of the ViewModel and observe the data using the getData() method.

MyViewModel myViewModel = new ViewModelProvider(requireActivity()).get(MyViewModel.class);
myViewModel.getData().observe(getViewLifecycleOwner(), new Observer() {
@OverRide
public void onChanged(@nullable String data) {
// update UI with the new data
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment