0

I have two activities named Main activity and Second Activity. Main activity has an event handler. I need to disable a button in second activity when an event occurs.

Main activity

public void myEventListener(int eventID){

  switch (eventID) {
  case : 0
   // disable button of second activity here
   break;
  }
}
touhid udoy
  • 4,005
  • 2
  • 18
  • 31

5 Answers5

0

In you First Activity make Boolean static variable.

Example:

FirstActivity

create a Boolean static global variable

public static Boolean clicked = false;

onFirstActivity if Event occurs.

event occurred => clicked = true; otherwise it is false

SecondActivity

in second activity get the value to static boolean from FirstActivity

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              if (FirstActivity.clicked){
                     //Do Nothing
              }else{
                     //Perform action
              }

        }
    });
Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
0

It's very simple to disable a button. Follow the below steps to achieve your problem.

  1. Define a global boolean value as "false"
  2. In onClickEvent override, the boolean value as "true".

Then check with the boolean value as follows

private boolean isClicked = false;

if(isClicked){
   button.disabled(true);
} else {
   button.disabled(false);
}

Please let me know if you have any issues while applying.

hemandroid
  • 568
  • 4
  • 12
  • i have different activites need to disable button of second activity when an event occure in first activity. – Muraleedharanpillai K Nov 09 '18 at 06:01
  • @MuraleedharanpillaiK You can simply pass the boolean value as true in Intent using putExtra. Then using a getIntent method in onCreate of the SecondActivity, you can pick the boolean value and place the if condition to disable and enable the button. You don't need to use any broadCastReceivers or hard methods to accomplish your requirement. If you find any difficulties, please let me know – hemandroid Nov 10 '18 at 15:40
  • can i pass value with putExtra in the middle of second activity is running. I know the answer is no. – Muraleedharanpillai K Nov 12 '18 at 04:46
  • I think you are confusing, please try to follow the below steps. 1. Create intent to move from Activity A to Activity B and in this intent pass the boolean value as true. 2. In second Activity B using getIntent method fetch the boolean value using the same key as you have been used in the intent in Activity A. syn: In second Activity B Intent intent = getIntent(); Boolean b = intent.getBooleanExtra("Key"); – hemandroid Nov 12 '18 at 04:54
0

first make reference of second activity and set button visibility GONE or INVISIBLE It's Work

SeconActivity sa;      //reference of second activity
public void myEventListener(int eventID){
switch (eventID) {
case : 0
sa.btnofsecondactivity.setVisibilty(View.GONE);
break;
}
}
0

You can go with LocalBroadCastManager.

in MainActivity wherever you want to trigger the method

LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("event-occured"));

in SecondActivity register the LocalBroadcastManager and receive it.

public class SecondActivity extends AppCompatActivity {

private BroadcastReceiver mainActivityReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    mainActivityReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // do whatever you want to do

            Log.d("TAG", "broadcast received");
        }
    };
    LocalBroadcastManager.getInstance(this).registerReceiver(mainActivityReceiver, new IntentFilter("main-activity-initialized"));
}


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

Don't forget to unregister the listener in SecondActivity's onDestroy method. Taken reference from here.

Satyajit
  • 625
  • 7
  • 12
0

This is an easy one.

  1. Use SharedPreference of changing data(boolean maybe) in MainAcitivity
  2. Use SharedPreference.OnSharedPreferenceChangeListener in SecondActivity for listening to that specific data and changing button state at runtime in.

MainActivity.java

public class MainActivity extends AppCompatActivity {

SharedPreferences.Editor editor;


public void myEventListener(int eventID){

    switch (eventID) {
        case  0:
            editor = getSharedPreferences("pref",MODE_PRIVATE).edit();
            editor.putBoolean("event",true);

        break;
    }
}

}

SecondActivity

public class SecondActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

}

@Override
protected void onStart() {
    super.onStart();

    sharedPreferences=getSharedPreferences("pref",MODE_PRIVATE);
    sharedPreferences.registerOnSharedPreferenceChangeListener(this);

}

@Override
protected void onStop() {
    super.onStop();
    sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);

}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if(key.equals("event") && sharedPreferences.getBoolean(key,false))
    {
        //add your code to disable your button or any action you want

    }
}
}
touhid udoy
  • 4,005
  • 2
  • 18
  • 31