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;
add a comment |
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;
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
add a comment |
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;
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;
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
add a comment |
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
add a comment |
5 Answers
5
active
oldest
votes
up vote
0
down vote
accepted
This is an easy one.
- Use
SharedPreferenceof changing data(boolean maybe) inMainAcitivity - Use
SharedPreference.OnSharedPreferenceChangeListenerinSecondActivityfor 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
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
add a comment |
up vote
0
down vote
It's very simple to disable a button. Follow the below steps to achieve your problem.
- Define a global boolean value as "false"
- 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.
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
add a comment |
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
);
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
add a comment |
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;
sorry it is not good idea to create an object of an activity having life cycles.
– Muraleedharanpillai K
Nov 9 at 6:34
add a comment |
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.
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
|
show 1 more comment
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.
- Use
SharedPreferenceof changing data(boolean maybe) inMainAcitivity - Use
SharedPreference.OnSharedPreferenceChangeListenerinSecondActivityfor 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
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
add a comment |
up vote
0
down vote
accepted
This is an easy one.
- Use
SharedPreferenceof changing data(boolean maybe) inMainAcitivity - Use
SharedPreference.OnSharedPreferenceChangeListenerinSecondActivityfor 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
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
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
This is an easy one.
- Use
SharedPreferenceof changing data(boolean maybe) inMainAcitivity - Use
SharedPreference.OnSharedPreferenceChangeListenerinSecondActivityfor 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
This is an easy one.
- Use
SharedPreferenceof changing data(boolean maybe) inMainAcitivity - Use
SharedPreference.OnSharedPreferenceChangeListenerinSecondActivityfor 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
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
add a comment |
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
add a comment |
up vote
0
down vote
It's very simple to disable a button. Follow the below steps to achieve your problem.
- Define a global boolean value as "false"
- 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.
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
add a comment |
up vote
0
down vote
It's very simple to disable a button. Follow the below steps to achieve your problem.
- Define a global boolean value as "false"
- 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.
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
add a comment |
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.
- Define a global boolean value as "false"
- 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.
It's very simple to disable a button. Follow the below steps to achieve your problem.
- Define a global boolean value as "false"
- 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.
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
add a comment |
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
add a comment |
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
);
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
add a comment |
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
);
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
add a comment |
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
);
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
);
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
add a comment |
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
add a comment |
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;
sorry it is not good idea to create an object of an activity having life cycles.
– Muraleedharanpillai K
Nov 9 at 6:34
add a comment |
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;
sorry it is not good idea to create an object of an activity having life cycles.
– Muraleedharanpillai K
Nov 9 at 6:34
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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.
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
|
show 1 more comment
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.
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
|
show 1 more comment
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.
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.
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
|
show 1 more comment
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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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