How to force to do when button clicked from other activity


How to force to do when button clicked from other activity



I want to toDo() work App.class (AllActivity) when button clicked from other example.class.(Activity)


public class Otherclass extend AppCompatActivity{

Button example = (Button)findViewById(R.id.option_language);
example.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
example.isChecked();
}
});
}

public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ToDo();
}


@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toDo();
}

public void toDo() {
if(new OtherClass().example.isChecked()) {
// To do something.
}
}
}
}



I wanted to do it. But, I got java.lang.NullPointerException: .isChecked()' on a null object reference





Other class is activity. It will not exist when application onCreate is triggered. You cannot create this class with 'new' acitvities can be only started with intents.
– 3mpty
Jul 2 at 10:09





Better to use an interface in this scenario. It will help to let you know if something happened in another activity
– Clint Paul
Jul 2 at 10:14





best way is using EventBus, check about it, its very easy and would fulfill your requirement
– Ashish Sharma
Jul 2 at 10:15





A possible solution would be to make toDo() static and encapsulate it in a non-activity class (this is not required, but it is good practice). But it depends a lot on what you want to to. What you're doing is bad design, though.
– Luís Henriques
Jul 2 at 10:15




3 Answers
3



This is a bad behavior that you implemented.



If you want to check whether the example button is clicked or not. Simply take a boolean flag in Your App class, and set it's value on the button click.


App



And if you wish to pass this flag from one activity to another simply use intent for it.


public class Otherclass extend AppCompatActivity{

Button example = (Button)findViewById(R.id.option_language);
example.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
App.setIsChecked(true);
}
});
}

public class App extends Application {
public static boolean isChecked = false;
@Override
public void onCreate() {
super.onCreate();
ToDo();
}

public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toDo();
}

public void toDo() {
if(isChecked) {
// To do something.
}
}
}
}





getintent() is deprecated. I can not use it. I don't know why.
– Fake Fakekovich
Jul 2 at 10:19





Where are you trying to getIntent()?
– Brijesh Joshi
Jul 2 at 10:21


getIntent()





It's not proper way. Check my updated answer
– Brijesh Joshi
Jul 2 at 10:24






Now, I got it. Thanks a lot.
– Fake Fakekovich
Jul 3 at 19:11



you can call todo method of App class on any button click like this


((App)getApplication()).todo();


public class Otherclass extend AppCompatActivity{
static Boolean checked=false;
Button example = (Button)findViewById(R.id.option_language);
example.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checked=true;
}
});
}

public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ToDo();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toDo();
}

public void toDo() {
if(new OtherClass().checked) {
// To do something.
}
}
}

}



Might this will be useful
This is bad coding practice to use static in class. Instead, use intents to communicate between class





You must declare Boolean checked=false; variable in the App class and static else it won't be accessible there.
– Brijesh Joshi
Jul 2 at 10:28


Boolean checked=false;


App





Rightly said, I thought app is an inner class of Otherclass.. My blunder mistake
– ChandraKumar
Jul 2 at 10:33






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy