M
M
mr_serg772017-08-29 16:37:16
Android
mr_serg77, 2017-08-29 16:37:16

What are the solutions to not recreate the Model class?

The actual code structure:
Activity:

public class MainActivity extends AppCompatActivity {

    private iMainActivityModel iModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iModel = new MainActivityModel(this);
    }
}

iMainActivityModel:
interface iMainActivityModel {

    boolean isFirstStart();

    void showSetupFragment();

    void showMainFragment();

}

MainActivityModel:
class MainActivityModel implements iMainActivityModel {

    private Activity activ;

    MainActivityModel(Activity activity){
        this.activ = activity;
    }

    @Override
    public boolean isFirstStart() {
        return false;
    }

    @Override
    public void showSetupFragment() {
     
    }

    @Override
    public void showMainFragment() {
       
    }
}

When the phone is turned over, the activity itself is recreated, the model is also recreated, because I initialize it in onCreate. If we say there is some method that will have to give something to the callback - at this moment the user turns the phone over - the data flies to the null pointer, error + re-creation = waste of reurs as for me.
Of course, you can do a check for null there, or some other hack. But I don't think it's a must have.
I'm looking at binding \ some loaders, but so far I'm not fully aware of the principle of use.
Push what to read / study? And in general, in terms of structure, if you show suitable examples of MVP \ MVVM - I will be grateful!
I'm not banned in Google, I just don't have a good idea what to google.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrei K, 2017-08-29
@mr_serg77

What do you want to do and where did you get this approach from?
Do you want to put all the logic in the Model and have it manage the Activity? Then it turns out Presenter from MVP. Read articles on the same hub about MVP.
In order not to manually deal with binding the Activity and the Presenter, you can use the Moxy library . She will take over the lifecycle management of the Presenter. There are excellent articles on Moxy on Habré.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question