我是一名新手,正在学习架构组件。我创建了一个ViewModel类并按照架构组件的要求实现了LifecycleObserver。在ViewModel类中,我重写了run()方法,并通过handler类帮助递归调用它,延迟执行。在这个方法中,images数组的值会发生变化,并被LiveData包装。
MainActivity观察images数组的变化,并且当值发生变化时,ImageView绑定了images数组的值。MainActivity也是生命周期所有者。
现在我想要实现Lifecycle Observer,这样当我暂停应用程序时,run()方法也应该暂停,当我恢复应用程序时,它应该从我暂停的地方开始。
我尝试通过在handler中创建暂停和恢复方法来实现,但没有成功。
请帮我解决这个问题。
MainActivity类
private ImageViewModel imageViewModel;
private ImageView imageView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDialogueBox();
}
private void showDialogueBox() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialogue_box);
dialog.setTitle("custom");
Button button =(Button)dialog.findViewById(R.id.button1);
button.setEnabled(true);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageViewModel = ViewModelProviders.of(MainActivity.this).get(ImageViewModel.class);
subscribe();
dialog.cancel();
}
});
dialog.show();
}
private void subscribe() {
final Observer<Integer> imageTimeObserver = new Observer<Integer>() {
@Override
public void onChanged(@Nullable Integer integer) {
imageView = findViewById(R.id.imageView);
imageView.setImageResource(integer);
getLifecycle().addObserver(new BrainalyseComponent(MainActivity.this,getApplicationContext(),imageViewModel));
}
};
final Observer<Integer> progressbarTimeObserver = new Observer<Integer>() {
@Override
public void onChanged(@Nullable Integer integer) {
progressBar = findViewById(R.id.progressBar);
progressBar.setProgress(integer);
}
};
imageViewModel.getImage().observe(this,imageTimeObserver);
imageViewModel.getProgressbarStatus().observe(this,progressbarTimeObserver);
}
ViewModel类
private int imagesIndex;
private int delay;
public Handler handler;
private MutableLiveData<Integer> imageLiveData = new MutableLiveData<>();
private MutableLiveData<Integer> progressbarLiveData = new MutableLiveData<>();
private int progressBarStatus;
private HashMap<Integer,Integer> imagesAndDelay;
private int images[] =
{
R.drawable.food_1,
R.drawable.food_2,
R.drawable.food_3,
R.drawable.food_4,
R.drawable.food_5,
R.drawable.food_6,
R.drawable.food_7,
R.drawable.food_8,
R.drawable.food_9,
R.drawable.food_10
};
public ImageViewModel(){
imagesIndex = 0;
progressBarStatus = 0;
delay = 2;//to be changed as delay will be discussed
handler = new Handler();
imagesAndDelay = new HashMap<>();
shuffleImages();
runnable.run();
}
private void shuffleImages() {
Random random = new Random();
for (int i = 0; i < images.length; i++) {
int j = random.nextInt(images.length);
int temp = images[i];
images[i] = images[j];
images[j] = temp;
}
}
public Runnable runnable = new Runnable() {
@Override
public void run() {
if (imagesIndex<images.length){
progressBarStatus += 100/images.length;
progressbarLiveData.postValue(progressBarStatus);
imageLiveData.postValue(images[imagesIndex]);
imagesAndDelay.put(images[imagesIndex],delay);
imagesIndex++;
delay += 2;
}else {
stopTask();
return;
}
handler.postDelayed(runnable,2000);
}
};
public Thread newThread = new Thread(){
public void run(){
if (imagesIndex<images.length){
progressBarStatus += 100/images.length;
progressbarLiveData.postValue(progressBarStatus);
imageLiveData.postValue(images[imagesIndex]);
imagesAndDelay.put(images[imagesIndex],delay);
imagesIndex++;
delay += 2;
}else {
stopTask();
return;
}
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
run();
}
}
};
public LiveData<Integer> getImage(){
return imageLiveData;
}
public LiveData<Integer> getProgressbarStatus(){
return progressbarLiveData;
}
private void stopTask() {
delay = 2;
progressBarStatus = 0;
imagesIndex = 0;
Utility.setImagesAndDelay(imagesAndDelay);
handler.removeCallbacks(runnable);
}
LifecycleObsever
private Context mContext;
private static final String LOG_TAG = BrainalyseComponent.class.getSimpleName();
private ImageView imageView;
private ProgressBar progressBar;
private Integer integer;
LifecycleOwner lifecycleOwner;
private ImageViewModel imageViewModel;
public BrainalyseComponent(LifecycleOwner lifecycleOwner, Context context, ImageViewModel imageViewModel) {
this.mContext = context;
this.imageView = imageView;
this.imageViewModel = imageViewModel;
this.integer = integer;
this.progressBar = progressBar;
this.lifecycleOwner = lifecycleOwner;
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume(){
Log.d(LOG_TAG,"on resume of app");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause(){
}