0

I have an activity that contains a View.

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_act);

        LinearLayout surface = (LinearLayout) findViewById(R.id.surfaceView1);
        surface.addView(new PlacingBoxView(this));
        surface.setBackgroundColor(Color.BLACK);

        if (ACTIONS_FINISHED){
            Log.d("TAG", "i made it!!");
        }
    }

Within the PlacingBoxView i do some actions, and once they are finished I set the a boolean called ACTIONS_FINISHED to true. My goal is once I've set this boolean to true then do some actions in the activity automatically.

I've been trying to use all this possibilities, but they are just to share information. Maybe I should use onActivityResult, but I'm already within the activity...

Any idea? THanks in advance!

EDIT: The view performs an AsyncTask and draws several shapes, but the user doesn't touch the screen at all

Community
  • 1
  • 1
Alvaro
  • 1,430
  • 2
  • 23
  • 41

2 Answers2

1

I've been trying to use all this possibilities, but they are just to share information. Maybe I should use onActivityResult, but I'm already within the activity...

No, onActivityResult() got different purpose. What you shall do is implement ordinary listener in your View and then attach your activity to view's listener (same way you do with i.e. OnClickListener). That way your view will be able to call back when needed and trigger some actions in Activity that way.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
1

Define a interface in your PlacingBoxView class:

public class PlacingBoxView{
   private GetCallBack callback;

   public PlacingBoxView(Context context){
         callback = (GetCallBack) context;
   }

   public void someMethodWhichSendsData(){
         callback.onMessageReceived(msg);
   }


   public interface GetCallBack{
       void onMessageReceived(Object msg);
   }
}

Now in your activity implement the interface:

public class SecondActivity extends AppCompatActivity implements PlacingBoxView.GetCallBack{

 @Override
 public void onMessageReceived(Object msg){
    // you have got your msg here.
 }


}
George Mulligan
  • 11,813
  • 6
  • 37
  • 50
Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
  • Perfect Interface! Given that I'm using a view and not an Activity in `PlacingBoxView` you should just change the `Àctivity activity` with `Context context` and I'll mark it right! Thank you for the help ;) – Alvaro Apr 05 '16 at 22:03
  • @Alvaro I made the change you requested in the answer so you can accept it. – George Mulligan Apr 05 '16 at 23:00