how can I finish activity from view

2

I have got an activity which has custom view.

I have to add activity result:

    public class ActView extends Activity implements OnClickListener {
<...code...>
@Override
    protected void onCreate(Bundle savedInstanceState) {
    <...code...>
     layout = (LinearLayout) findViewById(R.id.chart_container);
        timelineview = new VDrawTimeLine(this,contentFull);
        timelineview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        timelineview.setBackgroundColor(0xffffffff);
        timelineview.setVisibility(View.VISIBLE);

        layout.addView(timelineview);

    <...code...>
    }
}

View class:

public class VDrawTimeLine extends View implements OnTouchListener{
<...code...>
public VDrawTimeLine(Context context, ArrayList<_MainData> contentFull) {
        super(context);
        this.con = context;
        this.content = contentFull;
        <...code...>
    }
}
@Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        finishFunction();//I need here finish event this activity result for ActView Activity.
        return true;
    }

finishFunction - what i have to write here to finish the ActView with the result for parent activity?

I need somethin like this:

Intent intent = new Intent();
    intent.putExtra(dataname, value);
    setResult(RESULT_OK,intent);
    finish();
java
android
view
asked on Stack Overflow Jul 14, 2013 by Vyacheslav

2 Answers

9

Since the Context you are using is the Activitys context, you can cast the views context to Activity and call finish() upon it.

For instance:

private void finishFunction() {
   Activity activity = (Activity)getContext();
   activity.finish();
}

Probably it is not the best choice from the design perspective

answered on Stack Overflow Jul 14, 2013 by Blackbelt
0

How to close an Activity from View Model class in MVVM Databinding in Android Kotlin.

You should pass the context/acitivty reference in your View Model class from the activity as below

private lateinit var registerViewModel: RegisterViewModel

initialize it onCreate method

registerViewModel = RegisterViewModel(this)

RegisterViewModel class which is extends to BaseObservable()

Write this below code to close activity on button click

fun handleOnClick(view: View) {
        when (view.id) {
            R.id.registerBTN -> {
                val activity = context as Activity
                activity.finish()
            }
            else -> Toast.makeText(context, "To-DO", Toast.LENGTH_SHORT).show()
        }
    }

You can define in your layout.xml file like below

<Button
                android:id="@+id/registerBTN"
                style="@style/Widget.AppCompat.Button.Colored"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:enabled="@{registerViewModel.isAllowRegister}"
                android:layout_marginVertical="32dp"
                android:text="@string/register"
                android:onClick="@{registerViewModel::handleOnClick}"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/passwordET" />

User contributions licensed under CC BY-SA 3.0