Android---ViewModel

在前面我们了解了横竖屏的切换会导致暂时数据的丢失。然后解决的办法是通过Bundle键值对的形式保存下来,再取出赋值。

而ViewModel也可以实现这样的效果。

接下来实现的效果是由两个按键的点击,可以对一个数进行变大,然后横竖屏的切换不会使得这个值初始化。
Android---ViewModel_第1张图片

先写一个类继承ViewModel:

public class MyViewModel extends ViewModel {
   public int number=0;

    @Override
    protected void onCleared() {
        super.onCleared();
        Log.e("TAG", "onCleared");
    }
}

核心代码:

    private MyViewModel viewModel;
    private Button btn_one;
    private Button btn_two;
    private TextView tv_one;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uitest);

        //布局初始化
        initView();
        
       
        viewModel=new ViewModelProvider(this).get(MyViewModel.class);
         tv_one.setText(String.valueOf(viewModel.number));
         
        //点击监听
        initBtn();
    }

    private void initBtn() {
        btn_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.number++;
                tv_one.setText(String.valueOf(viewModel.number));
            }
        });

        btn_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.number+=2;
                tv_one.setText(String.valueOf(viewModel.number));
            }
        });
    }

    private void initView() {

        btn_one=(Button)findViewById(R.id.btn_one);
        btn_two=(Button)findViewById(R.id.btn_two);
        tv_one=(TextView)findViewById(R.id.tv_one);
    }

但是上面这种方法需要添加依赖:

 implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

才能使用:

viewModel=new ViewModelProvider(this).get(MyViewModel.class);

如果不选择添加依赖则没有

viewModel=new ViewModelProvider(this).get(MyViewModel.class);

则需要使用(多了一个工厂参数):
用法:

private  ViewModelProvider.AndroidViewModelFactory factory;
 viewModel=new ViewModelProvider(this,factory).get(MyViewModel.class);
 factory = new ViewModelProvider.AndroidViewModelFactory(getApplication());

或者:

 viewModel=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(MyViewModel.class);

整体代码:

  private MyViewModel viewModel;
    private Button btn_one;
    private Button btn_two;
    private TextView tv_one;
    private  ViewModelProvider.AndroidViewModelFactory factory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uitest);

        //布局初始化
        initView();
        viewModel=new ViewModelProvider(this,factory).get(MyViewModel.class);
//        viewModel=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(MyViewModel.class);
        tv_one.setText(String.valueOf(viewModel.number));

        //点击监听
        initBtn();
    }

    private void initBtn() {
        btn_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.number++;
                tv_one.setText(String.valueOf(viewModel.number));
            }
        });

        btn_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.number+=2;
                tv_one.setText(String.valueOf(viewModel.number));
            }
        });
    }

    private void initView() {
        factory = new ViewModelProvider.AndroidViewModelFactory(getApplication());
        btn_one=(Button)findViewById(R.id.btn_one);
        btn_two=(Button)findViewById(R.id.btn_two);
        tv_one=(TextView)findViewById(R.id.tv_one);
    }

你可能感兴趣的:(Android)