Two different SwitchCompats in two different activities - java

I have 2 different SwithCompat in 2 different activity, what i want to achieve is, if i click the SwitchCompat in Activity A, the SwitchCompat in Acitivity B will be clicked as well. Vice Versa.
Can i achieve this with this code :
aSwitch = findViewById(R.id.switchs);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//do something
}
}
});

first of all when you are pass bundle for open second activity pass with bundle
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
and get bundle in second activity
boolean value = getIntent().getExtras().getBoolean(key);
according to boolean value you can change switch on and off

You can define interface as a callback in activity one use to activity two like this :
public class ActivityOne{
private ICallback mICallback;
private SwitchCompat mSwitchButton;
public interface ICallback {
void getData(boolean state);
}
mSwitchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
mICallback.getData(isChecked);
}
});
}
And in TwoActivity:
public class TwoActivity implements OneActivity.ICallback{
#Override
public void getData(boolean state) {
//YOU CAN USE IT HERE IN SECOND ACTIVITY
}
}
Maybe you face to callback is null in first activity then you should pass context when you going to second activity from first activity.

Related

What am i doing wrong when I try to call an activity outside an other activity in Android Java?

I Want to put all my methods in seperate classes to clean my code up when creating my Android App but i can seem to get it right.
In my MainActivity class i call the method from the importet class noteFunctionality onLongpress and OnDoubleTap.
#Override
public void onLongPress(#NonNull MotionEvent e) {
noteFunc.enterNote("2");
super.onLongPress(e);
}
#Override
public boolean onDoubleTap(#NonNull MotionEvent e) {
noteFunc.enterNote(2);
return super.onDoubleTap(e);
}
Then in my NoteFunctionality class i try start the a new activity class with the given int for the specifik activity, "I have more then one activity".
public void enterNote(int i) {
Class mainActivity = Class.forName("MainActivity" + i);
Intent secondActivityIntent = new Intent(this, mainActivity.class);
startActivity(secondActivityIntent);
}
What am I doing wrong?.
public void enterNote(Activity activity,Context context) {
//Class mainActivity = Class.forName("MainActivity" + i);
Intent secondActivityIntent = new Intent(context, activity.getClass());
startActivity(secondActivityIntent);
}
and when you call it
noteFunc.enterNote(MainActivity,yourActivity.this);
If you would like to trigger another Activity, it must be done through Context.
Therefore, you need to pass Context to your NoteFunctionality function to achieve your task.
MainActivity:
public class MainActivity extends Activity { // or extending any other form of Activity
NoteFunctionality noteFunc; // I assume you have properly assign it somewhere
#Override
public void onLongPress(#NonNull MotionEvent e) {
// You will have to pass also Context to enterNote function
noteFunc.enterNote(MainActivity.this, "2"); // You should pass an Integer instead of String here
super.onLongPress(e);
}
#Override
public boolean onDoubleTap(#NonNull MotionEvent e) {
noteFunc.enterNote(MainActivity.this, 2);
return super.onDoubleTap(e);
}
}
NoteFunctionality:
public class NoteFunctionality {
// You need to have Context here
public void enterNote(Context context, int i) {
Intent secondActivityIntent = new Intent(context, SecondActivity.class); // Replace your target Activity name with SecondActivity
context.startActivity(secondActivityIntent);
}
}

Set Receive Intent back to Default Value in Android using Java

FirstActivity.java
startActivity(new Intent(FirstActivity.this, SecondActivity.class)
.putExtra("passed_value", true));
SecondActivity.java
#Override
public void onPageFinished(final WebView view, String url) {
if(getIntent().getBooleanExtra("passed_value", false)){
}
}
After receiving intent value from FirstActivity.java every reload it runs the if block . But i need this intent to run only in onPageFinished method.
So how to run if block only when it comes from FirstActivity.java. Is there any way so that i can make this intent value back to default value after if block executed?
In your SecondActivity create a class level variable as boolean passed_value = false;
Then in your onCreate(), change its value like - passed_value = getIntent().getBooleanExtra("passed_value", false);
Finally you can use it like -
#Override
public void onPageFinished(final WebView view, String url) {
if(passed_value){
}
}
A fairly straight forward method would be to have a Boolean passedValue variable that you instantiate with the intent extra in onCreate, and then set as false in the if block. Something along the following lines should work,
public class SecondActivity extends AppCompatActivity {
Boolean passedValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
passedValue = getIntent().getBooleanExtra("passed_value", false);
//Other code things
}
#Override
public void onPageFinished(final WebView view, String url) {
if(passedValue){
//Do what you need to do
passedValue = false
}
}
}

How do I call one method of an activity from another class

This is a part of my activity class,
public class StatusActivity extends AppCompatActivity {
private boolean cFlag = false;
public boolean getFlag() { return cFlag; }
public void setFlag(boolean cFlag) {
this.cFlag = cFlag;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, messages);
ListView listView = findViewById(android.R.id.list);
listView.setAdapter(adapter);
adapters.add(adapter);
Button btn = findViewById(R.id.btnCustomerCheckIn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setFlag(true);
cFlag = getFlag();
Intent intent = new Intent(StatusActivity.this, MainActivity.class);
Toast.makeText(StatusActivity.this, "customer checked in",
Toast.LENGTH_LONG).show();
startActivity(intent);
}
});
}
this is a part of another class named as position
public class Position {
StatusActivity statusactivity = new StatusActivity();
public boolean ccflag = statusactivity.getFlag();
statusactivity.setFlag(false);
}
when i am calling
statusactivity.setFlag(false);
it is showing an error. couldn't recognize that what is the error that i am getting. but
statusactivity.getFlag();
is working properly. any help is appreciated
StatusActivity statusactivity = new StatusActivity();
This is totally wrong because you are trying to create a new Instance of activity.
If you want to use "setFlag" method from other activity then you must create a static method inside StatusActivity so you can access using directly StatusActivity.
And If you want to call from any fragment of this activity then please get an instance of this activity by the cast from "getActivity()" to StatusActivity and use that instance for call "setFlag" or "getFlag" method.
You can implement like below in Activity.
private static boolean cFlag = false;
public static boolean getFlag() {
return cFlag;
}
public static void setFlag(boolean cFlag) {
StatusActivity.cFlag = cFlag;
}
and call from position class like below
public class Position {
public boolean ccflag = StatusActivity.getFlag();
StatusActivity.setFlag(false);
}
you can not instantiate Activity class. if you want to call a method from activity, fist you should check that activity already running and not destroyed then by having the context of your class you just cast it like below then use its method
StatusActivity statusactivity= (StatusActivity )context;
statusactivity.setFlag(false);

Unable to get a reference for quick settings tile

I can set the state of the qs tile using the onClick() method but I would also like to toggle its state using a switch in my Main Activity. However, I can't change the state of the tile using a switch because getQsTile returns null outside the onClick() method.
public class AwesomeTileService extends TileService{
Tile tile;
public Activity activity;
public AwesomeTileService(Activity activity){
this.activity=activity;
Switch switch1 = (Switch)this.activity.findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
tile=getQsTile(); //tile is null
SwitchState(); //SwitchState does not work with onCheckedChanged
Log.v("Switch State=", ""+isChecked);
}
});
}
public AwesomeTileService(){
//zero arg constructor
}
#Override
public void onTileAdded() {
tile=getQsTile();
tile.setState(Tile.STATE_INACTIVE);
tile.updateTile();
}
#Override
public void onClick() {
tile=getQsTile(); //tile is not null
SwitchState(); //SwitchState method works with onClick
}
public void SwitchState(){
if(tile!=null){
if (tile.getState() == Tile.STATE_INACTIVE) {
tile.setState(Tile.STATE_ACTIVE);
}
else{
tile.setState(Tile.STATE_INACTIVE);
}
tile.updateTile();
}
}
}
I can simply remove the switch from the app but I'm curious to know what the issue is here and how I can overcome it.
If you want to update the tile programmatically from another activity, use sharedPreferences and store boolean value and write code in onStartListening method in tile service class to update tile. (onStartListening is called every time you swipe down the panel).

How do I pass a boolean value from my Main Activity to a fragment?

I am trying to pass a boolean value to a map fragment so that location updates will be requested only when this boolean value is false.
The value of this boolean value changes on the event of a button click.
RequestLocationUpdatesListener (interface)
public interface RequestLocationUpdatesListener {
void onRequestLocationUpdates(boolean recording);
}
Requesting Location Updates (Main Activity)
// Turn on location updates and pass to fragment
mButtonRecord.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG_CONTEXT, "Start Recording clicked.");
mRequestLocationUpdates = true; // boolean value
mRequestingLocationUpdates = new RequestLocationUpdatesListener() {
#Override
public void onRequestLocationUpdates(boolean recording) {
recording = mRequestLocationUpdates;
mRequestingLocationUpdates.onRequestLocationUpdates(recording);
}
};
}
});
Map Fragment
The Log.i is never accessed in my MapFragment, what am I missing?
#Override
public void onRequestLocationUpdates(boolean recording) {
Log.i(TAG_CONTEXT, "Recording? = " + recording);
}
Your problem is that you a creating a brand new listener in your onClick event everytime, which goes nowhere.
You need to have your mRequestingLocationUpdates variable pointing to your fragment that implements your interface. So, wherever you create your MapFragment you need to set this variable to the MapFragment.
For example, if you create your MapFragment in onCreate then it would go as follows:
#Override
protected void onCreate() {
// Other code
MapFragment mapFrag = new MapFragment();
// Now register your fragment as the listener
mRequestingLocationUpdates = mapFrag;
// Other code..
}
Now you have a reference to your fragment and can call this from your onClick event like so:
// Turn on location updates and pass to fragment
mButtonRecord.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG_CONTEXT, "Start Recording clicked.");
mRequestLocationUpdates = true; // boolean value
mRequestingLocationUpdates.onRequestLocationUpdates( mRequestLocationUpdates );
}
});

Categories

Resources