Android databinding activity completed ()
I try to use the databinding library to implement MVVM in my application. I can find a solution to the simple tasks I have completed, but the problem is that I can't complete the activity after completing some operations
Question:
After receiving a specific broadcast, I must close the activity of ViewModel class. Since VM class has no reference to view, how can I complete the activity? Specifically, I have a startup screen and corresponding VM class, which starts intentservice to download data. After downloading data, I must complete the startup screen and start mainactivity. I have found a way to start a new activity from VM, but it is mysterious to complete the previous one
Can you help me? thank you!
resolvent:
Create a splashstatus model using observableboolean:
private static class SplashStatus {
public final ObservableBoolean isFinished = new ObservableBooelan();
}
This is your splash layout:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="status" type="com.example.SplashStatus"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Splash Screen"
android:onFinish="@{status.isFinished}"/>
</LinearLayout>
</layout>
And bind adapter methods:
@BindingAdapter("android:onFinish")
public static void finishSplash(View view, boolean isFinished) {
if(isFinished){
((Activity)(view.getContext())).startActivity(new Intent(view.getContext(), MainActivity.class))
((Activity)(view.getContext())).finish();
}
}
In splashactivity.java, initialize data on oncreate. Each time you specify the onfinished.set (true) onfinished method, your mainactivity will start and complete the current