Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

java - Pass object between fragments using shared ViewModel

I'm trying to insert a object (repo) into the database and pass it to the next fragment.

I get the id of the inserted object and then I try to get the entity by the id, so the object includes the id. onLoaded() should set the object in the ViewModel so I can use it in the next fragment.

The problem: Sometimes onLoaded seems to be called late or not at all. And then I can't add more information in the second fragment.

How can I make sure 'onLoaded' is called before I use the second fragment? Or is there a better approach I should use? Maybe only passing the id in the ViewModel?

Fragment 1:

private void actionCloneRepo(@NonNull View view) {
        TextInputEditText edittext_username = view.findViewById(R.id.edittext_username);
        TextInputEditText edittext_token = view.findViewById(R.id.edittext_token);
        TextInputEditText edittext_git_name = view.findViewById(R.id.edittext_git_name);
        TextInputEditText edittext_git_email = view.findViewById(R.id.edittext_git_email);

        Repo repo = new Repo(
                Objects.requireNonNull(edittext_url.getText()).toString(),
                Objects.requireNonNull(edittext_username.getText()).toString(),
                Objects.requireNonNull(edittext_token.getText()).toString(),
                Objects.requireNonNull(edittext_git_name.getText()).toString(),
                Objects.requireNonNull(edittext_git_email.getText()).toString()
                );
        repoViewModel = new ViewModelProvider(requireActivity()).get(RepoViewModel.class);

        int id = (int) repoViewModel.insert(repo);
        repoViewModel.getRepoById(id).observe(getViewLifecycleOwner(), this::onLoaded);

        NavHostFragment.findNavController(AddProject1Fragment.this)
                .navigate(R.id.action_AddProject1Fragment_to_AddProject2Fragment);
    }

    private void onLoaded(Repo repo){
        repoViewModel.setCurrentRepo(repo);
    }

ViewModel:

public class RepoViewModel extends AndroidViewModel {

    private final RepoRepository repoRepository;
    private final LiveData<List<Repo>> allRepos;
    private Repo currentRepo;

    public RepoViewModel(Application application) {
        super(application);
        repoRepository = new RepoRepository(application);
        allRepos = repoRepository.getAllRepos();
    }

    public Repo getCurrentRepo() {
        return currentRepo;
    }

    public void setCurrentRepo(Repo currentRepo) {
        this.currentRepo = currentRepo;
    }

    public LiveData<Repo> getRepoById(int id) {
        return repoRepository.getRepoById(id);
    }

    public LiveData<List<Repo>> getAllRepos() {
        return allRepos;
    }

    public long insert(Repo repo) {
        return repoRepository.insert(repo);
    }

    public void update(Repo repo) {
        repoRepository.update(repo);
    }

    public void delete(Repo repo) {
        repoRepository.delete(repo);
    }
}
question from:https://stackoverflow.com/questions/65889134/pass-object-between-fragments-using-shared-viewmodel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...