Skip to content Skip to sidebar Skip to footer

Starting A 3rd Activity Setting Up Interfaces And Listeners

So my MainActivity was able to start my second activity from a btn click fine. Now on my second activity, I have a drag and drop type game where what I want is one the correct imag

Solution 1:

@Mark, It is not suggestible to implement the architecture in the above way. It is better to use an Interface to acheive this.

Step 1: Create an Interface.

publicinterfaceCorrectAnswerListener {

voidonRightAnswerSelected();

}

Step 2:

publicclassDropSpotextendsMyAbsoluteLayout
implements DropTarget, DragController.DragListener
{  // codes...public CorrectAnswerListener mListener; // Set This attribute from //DragActivityV2if (controller != null) {
   controller.setDragListener (this);
   controller.addDropTarget (this);
    System.out.println("Correct Spot");

    //DragActivityV2 ca = new DragActivityV2();//ca.correctAns();if(mListener != null) {
mListener.onRightAnswerSelected();
}
}

Step 3:

publicclassDragActivityV2extendsActivityimplementsView.OnLongClickListener, View.OnClickListener, View.OnTouchListener, CorrectAnswerListener;
    {

@overridepublicvoidonCreate() {

setContentView();

//Where ever you create an object to helper class, initiate the value as
helperClassObject.mListener = DragActivityV2.this;
}      
//more codes...//public void correctAns()//  {//    Context context;//  Intent intent = new Intent(this, FallAnimatio//nActivity.class);//DragActivityV2.this.startActivity(intent);//finish();@overridepublicvoidonRightAnswerSelected() {
Intentintent=newIntent(this, FallAnimatio//nActivity.class);
startActivity(intent);
finish();
}

Solution 2:

Do not instantiate Activity with new Activty()

On the DropSpot class get the context of FirstActivity,

publicclassDropSpotextendsMyAbsoluteLayout
implements DropTarget, DragController.DragListener
{  // codes...if (controller != null) {
   controller.setDragListener (this);
   controller.addDropTarget (this);
    System.out.println("Correct Spot");

    context.startActivtiy(context,DragActivityV2.class);

}

On The DragActivityV2onCreate call the correctAns()

publicclassDragActivityV2extendsActivityimplementsView.OnLongClickListener, View.OnClickListener, View.OnTouchListener
    {       //more codes...@OverrideprotectedvoidonCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

////code 
correctAns();
    }


publicvoidcorrectAns()

        {
            Context context;
            Intentintent=newIntent(this, FallAnimationActivity.class);
            DragActivityV2.this.startActivity(intent);
            finish();
        }

Post a Comment for "Starting A 3rd Activity Setting Up Interfaces And Listeners"