Disable a button when an event occure in main activity









up vote
0
down vote

favorite












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;











share|improve this question























  • does main activity always comes before second activity?
    – Touhidul Islam
    Nov 9 at 5:26










  • yes. second activity comes only after main activity.
    – Muraleedharanpillai K
    Nov 9 at 6:07










  • check this answer and let me know if it solves your problem stackoverflow.com/a/53221060/7360848
    – Touhidul Islam
    Nov 9 at 6:55










  • You can use the global variable from the file "AppConstants" and use that to set the button enable or disable, by changing the variable status true/false when your event is inprogress/done.
    – Sandeep Insan
    Nov 9 at 7:10














up vote
0
down vote

favorite












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;











share|improve this question























  • does main activity always comes before second activity?
    – Touhidul Islam
    Nov 9 at 5:26










  • yes. second activity comes only after main activity.
    – Muraleedharanpillai K
    Nov 9 at 6:07










  • check this answer and let me know if it solves your problem stackoverflow.com/a/53221060/7360848
    – Touhidul Islam
    Nov 9 at 6:55










  • You can use the global variable from the file "AppConstants" and use that to set the button enable or disable, by changing the variable status true/false when your event is inprogress/done.
    – Sandeep Insan
    Nov 9 at 7:10












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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;











share|improve this question















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;








android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 6:07









Touhidul Islam

430111




430111










asked Nov 9 at 5:08









Muraleedharanpillai K

33




33











  • does main activity always comes before second activity?
    – Touhidul Islam
    Nov 9 at 5:26










  • yes. second activity comes only after main activity.
    – Muraleedharanpillai K
    Nov 9 at 6:07










  • check this answer and let me know if it solves your problem stackoverflow.com/a/53221060/7360848
    – Touhidul Islam
    Nov 9 at 6:55










  • You can use the global variable from the file "AppConstants" and use that to set the button enable or disable, by changing the variable status true/false when your event is inprogress/done.
    – Sandeep Insan
    Nov 9 at 7:10
















  • does main activity always comes before second activity?
    – Touhidul Islam
    Nov 9 at 5:26










  • yes. second activity comes only after main activity.
    – Muraleedharanpillai K
    Nov 9 at 6:07










  • check this answer and let me know if it solves your problem stackoverflow.com/a/53221060/7360848
    – Touhidul Islam
    Nov 9 at 6:55










  • You can use the global variable from the file "AppConstants" and use that to set the button enable or disable, by changing the variable status true/false when your event is inprogress/done.
    – Sandeep Insan
    Nov 9 at 7:10















does main activity always comes before second activity?
– Touhidul Islam
Nov 9 at 5:26




does main activity always comes before second activity?
– Touhidul Islam
Nov 9 at 5:26












yes. second activity comes only after main activity.
– Muraleedharanpillai K
Nov 9 at 6:07




yes. second activity comes only after main activity.
– Muraleedharanpillai K
Nov 9 at 6:07












check this answer and let me know if it solves your problem stackoverflow.com/a/53221060/7360848
– Touhidul Islam
Nov 9 at 6:55




check this answer and let me know if it solves your problem stackoverflow.com/a/53221060/7360848
– Touhidul Islam
Nov 9 at 6:55












You can use the global variable from the file "AppConstants" and use that to set the button enable or disable, by changing the variable status true/false when your event is inprogress/done.
– Sandeep Insan
Nov 9 at 7:10




You can use the global variable from the file "AppConstants" and use that to set the button enable or disable, by changing the variable status true/false when your event is inprogress/done.
– Sandeep Insan
Nov 9 at 7:10












5 Answers
5






active

oldest

votes

















up vote
0
down vote



accepted










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









share|improve this answer






















  • This code does not calling "disableMyButton()" .
    – Muraleedharanpillai K
    Nov 9 at 8:34










  • I edited my code, this was just a place holder, meaning to be replaced by your own custom action
    – Touhidul Islam
    Nov 9 at 8:49










  • onSharedPreferenceChanged is not occuring.
    – Muraleedharanpillai K
    Nov 9 at 8:56










  • i used similar approach many times, it must work, check your implantation
    – Touhidul Islam
    Nov 9 at 9:35

















up vote
0
down vote













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.






share|improve this answer




















  • i have different activites need to disable button of second activity when an event occure in first activity.
    – Muraleedharanpillai K
    Nov 9 at 6: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 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 at 4: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 at 4:54


















up vote
0
down vote













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



);





share|improve this answer






















  • I can't use intent because event occure only after the second activity started.
    – Muraleedharanpillai K
    Nov 9 at 6:00










  • okay. global static variable is perfect for you.
    – Ali Ahmed
    Nov 9 at 6:01










  • but how i know the event is occured or not in second activity?
    – Muraleedharanpillai K
    Nov 9 at 6:03










  • when button is clicked you can fetch the static Boolean variable of FirstActivity. See my code,,
    – Ali Ahmed
    Nov 9 at 6:05










  • but i need it without clicking a button . I mean When the event occure it automatically disable the button
    – Muraleedharanpillai K
    Nov 9 at 6:32

















up vote
0
down vote













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;







share|improve this answer




















  • sorry it is not good idea to create an object of an activity having life cycles.
    – Muraleedharanpillai K
    Nov 9 at 6:34

















up vote
0
down vote













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.






share|improve this answer






















  • mListener null pointer exception occured.
    – Muraleedharanpillai K
    Nov 9 at 7:58










  • because mListner is not initialized
    – Ali Ahmed
    Nov 9 at 8:00










  • how to solve that problem?
    – Muraleedharanpillai K
    Nov 9 at 8:12










  • Updated the answer with localBroadcastManager. Have a look. Let me know if you still face any issues.
    – Satyajit Das
    Nov 9 at 9:27










  • thank you.it's working
    – Muraleedharanpillai K
    Nov 9 at 11:07










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220177%2fdisable-a-button-when-an-event-occure-in-main-activity%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























5 Answers
5






active

oldest

votes








5 Answers
5






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










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









share|improve this answer






















  • This code does not calling "disableMyButton()" .
    – Muraleedharanpillai K
    Nov 9 at 8:34










  • I edited my code, this was just a place holder, meaning to be replaced by your own custom action
    – Touhidul Islam
    Nov 9 at 8:49










  • onSharedPreferenceChanged is not occuring.
    – Muraleedharanpillai K
    Nov 9 at 8:56










  • i used similar approach many times, it must work, check your implantation
    – Touhidul Islam
    Nov 9 at 9:35














up vote
0
down vote



accepted










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









share|improve this answer






















  • This code does not calling "disableMyButton()" .
    – Muraleedharanpillai K
    Nov 9 at 8:34










  • I edited my code, this was just a place holder, meaning to be replaced by your own custom action
    – Touhidul Islam
    Nov 9 at 8:49










  • onSharedPreferenceChanged is not occuring.
    – Muraleedharanpillai K
    Nov 9 at 8:56










  • i used similar approach many times, it must work, check your implantation
    – Touhidul Islam
    Nov 9 at 9:35












up vote
0
down vote



accepted







up vote
0
down vote



accepted






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









share|improve this answer














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










share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 8:48

























answered Nov 9 at 6:50









Touhidul Islam

430111




430111











  • This code does not calling "disableMyButton()" .
    – Muraleedharanpillai K
    Nov 9 at 8:34










  • I edited my code, this was just a place holder, meaning to be replaced by your own custom action
    – Touhidul Islam
    Nov 9 at 8:49










  • onSharedPreferenceChanged is not occuring.
    – Muraleedharanpillai K
    Nov 9 at 8:56










  • i used similar approach many times, it must work, check your implantation
    – Touhidul Islam
    Nov 9 at 9:35
















  • This code does not calling "disableMyButton()" .
    – Muraleedharanpillai K
    Nov 9 at 8:34










  • I edited my code, this was just a place holder, meaning to be replaced by your own custom action
    – Touhidul Islam
    Nov 9 at 8:49










  • onSharedPreferenceChanged is not occuring.
    – Muraleedharanpillai K
    Nov 9 at 8:56










  • i used similar approach many times, it must work, check your implantation
    – Touhidul Islam
    Nov 9 at 9:35















This code does not calling "disableMyButton()" .
– Muraleedharanpillai K
Nov 9 at 8:34




This code does not calling "disableMyButton()" .
– Muraleedharanpillai K
Nov 9 at 8:34












I edited my code, this was just a place holder, meaning to be replaced by your own custom action
– Touhidul Islam
Nov 9 at 8:49




I edited my code, this was just a place holder, meaning to be replaced by your own custom action
– Touhidul Islam
Nov 9 at 8:49












onSharedPreferenceChanged is not occuring.
– Muraleedharanpillai K
Nov 9 at 8:56




onSharedPreferenceChanged is not occuring.
– Muraleedharanpillai K
Nov 9 at 8:56












i used similar approach many times, it must work, check your implantation
– Touhidul Islam
Nov 9 at 9:35




i used similar approach many times, it must work, check your implantation
– Touhidul Islam
Nov 9 at 9:35












up vote
0
down vote













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.






share|improve this answer




















  • i have different activites need to disable button of second activity when an event occure in first activity.
    – Muraleedharanpillai K
    Nov 9 at 6: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 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 at 4: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 at 4:54















up vote
0
down vote













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.






share|improve this answer




















  • i have different activites need to disable button of second activity when an event occure in first activity.
    – Muraleedharanpillai K
    Nov 9 at 6: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 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 at 4: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 at 4:54













up vote
0
down vote










up vote
0
down vote









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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 5:27









hemandroid

317




317











  • i have different activites need to disable button of second activity when an event occure in first activity.
    – Muraleedharanpillai K
    Nov 9 at 6: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 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 at 4: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 at 4:54

















  • i have different activites need to disable button of second activity when an event occure in first activity.
    – Muraleedharanpillai K
    Nov 9 at 6: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 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 at 4: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 at 4:54
















i have different activites need to disable button of second activity when an event occure in first activity.
– Muraleedharanpillai K
Nov 9 at 6:01




i have different activites need to disable button of second activity when an event occure in first activity.
– Muraleedharanpillai K
Nov 9 at 6: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 at 15:40




@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 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 at 4:46




can i pass value with putExtra in the middle of second activity is running. I know the answer is no.
– Muraleedharanpillai K
Nov 12 at 4: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 at 4:54





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 at 4:54











up vote
0
down vote













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



);





share|improve this answer






















  • I can't use intent because event occure only after the second activity started.
    – Muraleedharanpillai K
    Nov 9 at 6:00










  • okay. global static variable is perfect for you.
    – Ali Ahmed
    Nov 9 at 6:01










  • but how i know the event is occured or not in second activity?
    – Muraleedharanpillai K
    Nov 9 at 6:03










  • when button is clicked you can fetch the static Boolean variable of FirstActivity. See my code,,
    – Ali Ahmed
    Nov 9 at 6:05










  • but i need it without clicking a button . I mean When the event occure it automatically disable the button
    – Muraleedharanpillai K
    Nov 9 at 6:32














up vote
0
down vote













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



);





share|improve this answer






















  • I can't use intent because event occure only after the second activity started.
    – Muraleedharanpillai K
    Nov 9 at 6:00










  • okay. global static variable is perfect for you.
    – Ali Ahmed
    Nov 9 at 6:01










  • but how i know the event is occured or not in second activity?
    – Muraleedharanpillai K
    Nov 9 at 6:03










  • when button is clicked you can fetch the static Boolean variable of FirstActivity. See my code,,
    – Ali Ahmed
    Nov 9 at 6:05










  • but i need it without clicking a button . I mean When the event occure it automatically disable the button
    – Muraleedharanpillai K
    Nov 9 at 6:32












up vote
0
down vote










up vote
0
down vote









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



);





share|improve this answer














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



);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 6:02

























answered Nov 9 at 5:19









Ali Ahmed

1,2831214




1,2831214











  • I can't use intent because event occure only after the second activity started.
    – Muraleedharanpillai K
    Nov 9 at 6:00










  • okay. global static variable is perfect for you.
    – Ali Ahmed
    Nov 9 at 6:01










  • but how i know the event is occured or not in second activity?
    – Muraleedharanpillai K
    Nov 9 at 6:03










  • when button is clicked you can fetch the static Boolean variable of FirstActivity. See my code,,
    – Ali Ahmed
    Nov 9 at 6:05










  • but i need it without clicking a button . I mean When the event occure it automatically disable the button
    – Muraleedharanpillai K
    Nov 9 at 6:32
















  • I can't use intent because event occure only after the second activity started.
    – Muraleedharanpillai K
    Nov 9 at 6:00










  • okay. global static variable is perfect for you.
    – Ali Ahmed
    Nov 9 at 6:01










  • but how i know the event is occured or not in second activity?
    – Muraleedharanpillai K
    Nov 9 at 6:03










  • when button is clicked you can fetch the static Boolean variable of FirstActivity. See my code,,
    – Ali Ahmed
    Nov 9 at 6:05










  • but i need it without clicking a button . I mean When the event occure it automatically disable the button
    – Muraleedharanpillai K
    Nov 9 at 6:32















I can't use intent because event occure only after the second activity started.
– Muraleedharanpillai K
Nov 9 at 6:00




I can't use intent because event occure only after the second activity started.
– Muraleedharanpillai K
Nov 9 at 6:00












okay. global static variable is perfect for you.
– Ali Ahmed
Nov 9 at 6:01




okay. global static variable is perfect for you.
– Ali Ahmed
Nov 9 at 6:01












but how i know the event is occured or not in second activity?
– Muraleedharanpillai K
Nov 9 at 6:03




but how i know the event is occured or not in second activity?
– Muraleedharanpillai K
Nov 9 at 6:03












when button is clicked you can fetch the static Boolean variable of FirstActivity. See my code,,
– Ali Ahmed
Nov 9 at 6:05




when button is clicked you can fetch the static Boolean variable of FirstActivity. See my code,,
– Ali Ahmed
Nov 9 at 6:05












but i need it without clicking a button . I mean When the event occure it automatically disable the button
– Muraleedharanpillai K
Nov 9 at 6:32




but i need it without clicking a button . I mean When the event occure it automatically disable the button
– Muraleedharanpillai K
Nov 9 at 6:32










up vote
0
down vote













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;







share|improve this answer




















  • sorry it is not good idea to create an object of an activity having life cycles.
    – Muraleedharanpillai K
    Nov 9 at 6:34














up vote
0
down vote













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;







share|improve this answer




















  • sorry it is not good idea to create an object of an activity having life cycles.
    – Muraleedharanpillai K
    Nov 9 at 6:34












up vote
0
down vote










up vote
0
down vote









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;







share|improve this answer












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;








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 6:20









Pansuriya Chirag

211




211











  • sorry it is not good idea to create an object of an activity having life cycles.
    – Muraleedharanpillai K
    Nov 9 at 6:34
















  • sorry it is not good idea to create an object of an activity having life cycles.
    – Muraleedharanpillai K
    Nov 9 at 6:34















sorry it is not good idea to create an object of an activity having life cycles.
– Muraleedharanpillai K
Nov 9 at 6:34




sorry it is not good idea to create an object of an activity having life cycles.
– Muraleedharanpillai K
Nov 9 at 6:34










up vote
0
down vote













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.






share|improve this answer






















  • mListener null pointer exception occured.
    – Muraleedharanpillai K
    Nov 9 at 7:58










  • because mListner is not initialized
    – Ali Ahmed
    Nov 9 at 8:00










  • how to solve that problem?
    – Muraleedharanpillai K
    Nov 9 at 8:12










  • Updated the answer with localBroadcastManager. Have a look. Let me know if you still face any issues.
    – Satyajit Das
    Nov 9 at 9:27










  • thank you.it's working
    – Muraleedharanpillai K
    Nov 9 at 11:07














up vote
0
down vote













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.






share|improve this answer






















  • mListener null pointer exception occured.
    – Muraleedharanpillai K
    Nov 9 at 7:58










  • because mListner is not initialized
    – Ali Ahmed
    Nov 9 at 8:00










  • how to solve that problem?
    – Muraleedharanpillai K
    Nov 9 at 8:12










  • Updated the answer with localBroadcastManager. Have a look. Let me know if you still face any issues.
    – Satyajit Das
    Nov 9 at 9:27










  • thank you.it's working
    – Muraleedharanpillai K
    Nov 9 at 11:07












up vote
0
down vote










up vote
0
down vote









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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 9:26

























answered Nov 9 at 6:42









Satyajit Das

13619




13619











  • mListener null pointer exception occured.
    – Muraleedharanpillai K
    Nov 9 at 7:58










  • because mListner is not initialized
    – Ali Ahmed
    Nov 9 at 8:00










  • how to solve that problem?
    – Muraleedharanpillai K
    Nov 9 at 8:12










  • Updated the answer with localBroadcastManager. Have a look. Let me know if you still face any issues.
    – Satyajit Das
    Nov 9 at 9:27










  • thank you.it's working
    – Muraleedharanpillai K
    Nov 9 at 11:07
















  • mListener null pointer exception occured.
    – Muraleedharanpillai K
    Nov 9 at 7:58










  • because mListner is not initialized
    – Ali Ahmed
    Nov 9 at 8:00










  • how to solve that problem?
    – Muraleedharanpillai K
    Nov 9 at 8:12










  • Updated the answer with localBroadcastManager. Have a look. Let me know if you still face any issues.
    – Satyajit Das
    Nov 9 at 9:27










  • thank you.it's working
    – Muraleedharanpillai K
    Nov 9 at 11:07















mListener null pointer exception occured.
– Muraleedharanpillai K
Nov 9 at 7:58




mListener null pointer exception occured.
– Muraleedharanpillai K
Nov 9 at 7:58












because mListner is not initialized
– Ali Ahmed
Nov 9 at 8:00




because mListner is not initialized
– Ali Ahmed
Nov 9 at 8:00












how to solve that problem?
– Muraleedharanpillai K
Nov 9 at 8:12




how to solve that problem?
– Muraleedharanpillai K
Nov 9 at 8:12












Updated the answer with localBroadcastManager. Have a look. Let me know if you still face any issues.
– Satyajit Das
Nov 9 at 9:27




Updated the answer with localBroadcastManager. Have a look. Let me know if you still face any issues.
– Satyajit Das
Nov 9 at 9:27












thank you.it's working
– Muraleedharanpillai K
Nov 9 at 11:07




thank you.it's working
– Muraleedharanpillai K
Nov 9 at 11:07

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220177%2fdisable-a-button-when-an-event-occure-in-main-activity%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages