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

4

2 回答 2

1

PlacingBoxView在你的类中定义一个接口:

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);
   }
}

现在在你activity实现接口:

public class SecondActivity extends AppCompatActivity implements PlacingBoxView.GetCallBack{

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


}
于 2016-04-05T21:45:18.597 回答
1

我一直在尝试使用所有这些可能性,但它们只是为了分享信息。也许我应该使用 onActivityResult,但我已经在活动中......

不,onActivityResult()有不同的目的。您应该做的是在您的视图中实现普通的监听器,然后将您的活动附加到视图的监听器(与您使用 ie 的方式相同OnClickListener)。这样,您的视图将能够在需要时回调并以这种方式触发 Activity 中的某些操作。

于 2016-04-05T21:31:58.600 回答