Created
January 7, 2019 17:44
-
-
Save NoNews/ef60ff69944efe14f5e28a7439169303 to your computer and use it in GitHub Desktop.
Work with fragments with transfer data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Dog implements Serializable { | |
private String name; | |
public Dog(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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<>();
}
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
}
});