java – Stop fragment's functions and calls and reset inner variables-ThrowExceptions

Exception or error:

In my app I have an activity and a fragment.

The fragment works with a parameter “A” that is given by the activity. It has its own arrays, etc. The fragment is moving among its different functions (from a function to another) and also it makes calls to asynchronous functions to receive files, read from a database, etc.

The situation is that I want, in a concrete moment when I push a button in mi activity, send a notification from my main activity to the fragment to stop all the processing and start from the begining the process with an original parameter “B”. That is, restart all the variables in the fragment, stop all the functions currently running, and all the asynchornous functions, etc, and start with the B parameter the whole process in the fragment.

My solution, that is not working is as follows:

  • I have a GetRequest(request) funciton in the fragment.

  • From the activity, I call this function, fixing a parameter request == B.

  • In each function of the fragment, I have a condition at the begining of those functions checking if (request != null). Then, if request != null I call a function in the fragment to restart all the inner variables, and call the first function, etc. Otherwise, I continue with the code normally.

The problem is that I think this doesn’t work with asynchornous functions, since some times I receive errors in variables that I reset to null and they are trying to read, and so on.

I’m sure this is not the best way to achieve this. Could you please help me?

Thanks!

How to solve:

I will suggest you to use LiveData with ViewModel in this case

For example:

First create a ViewModel class

YourViewModel.java:

//initialize this mutableLiveData first
public MutableLiveData<String> parameterA = new MutableLiveData<>("");

private void setParameterA(String myValue){
    parameterA.setValue(myValue);
}

Then in YourActivity.java,get the ViewModel class

YourViewModel viewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {

   ... all other thing 

   viewModel = ViewModelProviders.of(this).get(YourViewModel.class);
}


//then whatever the situation in activity,u want to update parameterA
private void SomeSituation() {
    //update the parameterA
    ...
    viewModel.setParameterA("Your value Here")
}

Then in your fragment,observe the value of parameterA

Your Fragment:

  // in onCreate() or onViewCreated()

@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState); 

  //get the same viewModel
  viewModel = ViewModelProviders.of(requireActivity()).get(YourViewModel.class);


  viewModel.parameterA.observe(getViewLifecycleOwner(), new Observer<String>() {
        @Override
        public void onChanged(String parameterA) {
            // anything changes on parameterA
            // do whatever you want with the value S
        }
    });
 }

So by the method above,whenever having the changes of parameterA in activity, fragment will get the update asynchornous,then you can do whatever you want with the update value inside onChanged() section.

As you mention in comment:

receive files (reads from a database), variables, arrays

LiveData can use for custom Model class as well,for example MutableLiveData<List<Post>>,so when you setValue() in activity,your fragment can get the value asynchornous in onChanged().

Before using LiveData,you need to Add Components to your Project

This is Android MVVM architecture,you can see the doc here,,example here

Leave a Reply

Your email address will not be published. Required fields are marked *