0

Store Activity:

  public void unlockcup2(){

    }

Main Activity:

public void unlock2(){
    x = (x-100);

    ImageView img5 = (ImageView) findViewById(R.id.bluecake);
    img5.setVisibility(View.VISIBLE);
}

Soo, I want to make it so when my button in Store activity(Witch calls unlockcup2()) Call unlock2 in my MainActivity . How do I do this?

1 Answers1

1

localbroadcastmanager can do this job. Send a broadcast in the StoreActivity and receive it in the MainActivity and call unlock2()

MainActivity (Receiver)

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // in this case, there's only one type of action, so no need to check action
            unlock2();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
                new IntentFilter("custom-action-name"));
    }

    @Override
    protected void onDestroy() {
        ...
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
    }

StoreActivity (Sender)

    public void unlockcup2() {
        ...
        LocalBroadcastManager.getInstance(this)
                .sendBroadcast(new Intent("custom-action-name"));
    }
Community
  • 1
  • 1
wrkwrk
  • 2,261
  • 15
  • 21