Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DataBinding Activity finish()

I am trying to implement MVVM in my app with DataBinding library. For simple tasks that I have accomplish I can find the way out, but problem is that I cannot finish activity after some action.

Problem:

After receiving specific broadcast I have to close activity from ViewModel class. Since VM class doesn't have reference of the View, how can I finish the activity? To be precise, I have splash screen and corresponding VM class for it that starts IntentService for downloading a data. After the data has been downloaded I have to finish the splash screen and start MainActivity. I have found the way to start new Activity from VM, but to finish the previous one is the mystery.

Can you please help me? Thanks!

like image 901
svarog Avatar asked Feb 21 '17 10:02

svarog


People also ask

Can I use both DataBinding and ViewBinding?

Data binding includes everything that ViewBinding has, so it wasn't designed to work side by side with View binding. The biggest issue is the naming conflict between the generated classes. Both ViewBinding and DataBonding would want to generate the class MainLayoutBinding for the layout main_layout. xml .

Can we use DataBinding in MVP?

Why use Data binding with Mvp? Combining Databinding along wih MVP pattern can result in a very clean structure and maintainable project. Databinding saves u a lot of stress and uneccesary long lines of code. Your UI is updated eaily and gone are those days where you need "findViewById" and onclick listeners and so on.


2 Answers

Create a SplashStatus model with a ObservableBoolean:

private static class SplashStatus {
   public final ObservableBoolean isFinished = new ObservableBooelan();
}

Here 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 binding adapter method:

@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 the SplashActivity.java init your data binding on onCreate. Whenever you assign isFinished.set(true) onFinished method will start your MainActivity and finish current.

like image 104
ugur Avatar answered Sep 21 '22 06:09

ugur


if you want just to finish() the activity from layout with databinding:

android:onClick="@{(view)->((Activity)(view.getContext())).finish()}"

like image 42
Fidan Bacaj Avatar answered Sep 20 '22 06:09

Fidan Bacaj