how can I trigger AsyncTasks that are contained in another activity from my main activity?
public class DatabaseActivity extends Activity {
private class DbReader extends AsyncTask<..> {
#Override
protected List<MyData> doInBackground(..) {
//execute query etc
}
}
private class DbSaver extends AsyncTask<..> {
#Override
protected void doInBackground(MyData data) {
//save to dn
}
}
private class DbRemover extends AsyncTask<..> {
#Override
protected void doInBackground(MyData data) {
//remove in db
}
}
}
How can I trigger from MyApplication extends Actitivy?
i think you should use seperate class where AsyncTask is alone.
when your app needs more than one AsyncTask then you should use seperate AsyncTask and call it.
private class CommonTask extends AsyncTask<..> {
public CommonTask(Foo foo){
}
#Override
protected void doInBackground(MyData data) {
//remove in db
}
}
No you can pass diffrent value for constructor and check what you want from Activity either data save or remove or anything else...
Related
I'm trying to get this example code to work in Java:
https://developer.android.com/training/basics/intents/result
private final ActivityResultLauncher<Void> mTakePicture =
registerForActivityResult(new TakePicturePreview(), mRegistry, new ActivityResultCallback<Bitmap>() {
#Override
public void onActivityResult(Bitmap thumbnail) {
mThumbnailLiveData.setValue(thumbnail);
}
});
The example happens to be a "Fragment", and it gets mRegistry from the constructor:
public class MyFragment extends Fragment {
private final ActivityResultRegistry mRegistry;
...
public MyFragment(#NonNull ActivityResultRegistry registry) {
super();
mRegistry = registry;
}
My test case is an Activity (the "MainActivity"), not a Fragment:
public class MainActivity extends AppCompatActivity {
...
private ActivityResultRegistry activityResultRegistry;
Q: How can I initialize my registry ("activityResultRegistry") in this scenario?
It was easier than I thought:
private ActivityResultRegistry activityResultRegistry = this.getActivityResultRegistry();
I'm trying to create couple of Java class to perform certain work. Let's say I want to get the task done by calling my classes like this:
FirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = task.getResult().getUser();
// ...
} else {
// Sign in failed, display a message and update the UI
Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
}
}
}
});
I could understand up to signInWithCredential(). I can't figure out how to implement addOnCompleteListener() and have a interface as argument.
I've currently create my top class like FirebaseAuth with methods like getInstance () and signInWithCredential(). Also, I tried creating an interface but I am getting error that result of the interface is never used. How can I implement the style of addOnCompleteListener(parameter 1, interface 2).
Here, addOnCompleteListener is getting parameters of activity and interface and in my case, I will be using the activity parameter for some work.
P.S: I found out this is called interface callback. If it's right, any guidance to it's structure will be great
You can do it like this:
Create an interface:
public interface onCompleteListener {
void onComplete(MyTask object);
}
Define your MyTask class:
public abstract class MyTask {
public abstract boolean someFunc1();
public abstract String someFunc2();
public abstract String someFunc3();
}
In your main class:
public class MainClass{
public static MainClass instance;
private static Activity mActivity;
public onCompleteListener onCompleteListener;
private MainClass(Activity activity) {
mActivity = activity;
}
public static synchronized MainClass getInstance(Activity activity) {
if (instance == null) {
instance = new MainClass(activity);
}
return instance;
}
public void addOnCompleteListener(#NonNull onCompleteListener var2) {
onCompleteListener = var2;
//Call your task function
doTask();
}
public void doTask(){
MyTask o = new MyTask() {
#Override
public boolean someFunc1() {
return true;
}
#Override
public String someFunc2() {
return "";
}
#Override
public String someFunc3 {
return "";
}
};
//Once done, pass your Task object to the interface.
onCompleteListener.onComplete(o);
}
}
Usage:
MainClass.getInstance(MainActivity.this).addOnCompleteListener(new onCompleteListener() {
#Override
public void onComplete(MyTask object) {
doYourWork(object);
}
});
Suppose I have defined a class with interface like this:
public class myClass {
public void test() {
//here I want to trigger `onStartListener`
}
interface OnStartListener {
public void onStart();
}
}
and class B I have defined like this:
public class ClassB implements myClass.OnStartListener {
public void ClassB() {
myClass test1 = new myClass();
myClass.test();
}
#Override
public void onStart() {
System.out.println("start triggered");
}
}
How can I trigger OnStartListener from test method of myClass so ClassB can handle it?
Yes, you need to subscribe your listener and call the method in the class A:
public class ClassB implements myClass.OnStartListener {
public void ClassB() {
myClass test1 = new myClass(this);
//test1.setListener(this);
myClass.test();
}
#Override
public void onStart() {
System.out.println("start triggered");
}
}
and
public class myClass {
OnStartListener myListener;
public myClass(OnStartListener myListener) {
this.myListener = myListener;
}
public void test() {
//here I want to trigger `onStartListener`
myListener.onStart();
}
interface OnStartListener {
public void onStart();
}
}
Have a look at how frameworks like swing handle listeners. Basically you need to "register" the listener instance (ClassB instance) with myClass and call onStart() on it.
ClassB would probably contain a List<OnStartListener> which is used in a loop and onStart() is called on each element. Registering would mean assing the instance of ClassB to that list.
My Code:
public class location
{
private class MyPhoneStateListener extends PhoneStateListener
{
//Get the Signal strength from the provider, each time there is an update
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
}
/*some text*/
}
how can i invoke "onSignalStrengthsChanged" method from "location" class.
You need to create a new MyPhoneStateListener instance and invoke the method on this instance.
For example:
public class location {
private class MyPhoneStateListener extends PhoneStateListener {
//Get the Signal strength from the provider, each time there is an update
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
}
/*some text*/
}
public void doSomething() {
PhoneStateListener listener = new MyPhoneStateListener();
listener.onSignalStrenghtsChanged(...);
}
}
Please notice that you can only create a MyPhoneStateListener instance in the location class because you defined the class private.
Also, notice that doSomething() belongs to location.
Assume the following two classes:
public class Network {
private static Network instance;
public Network() {
instance = this;
}
public static Network getInstance() {
return instance;
}
public interface Listener {
public void event(String msg);
};
ArrayList<Listener> listeners = new ArrayList<Listener>();
public void addListener(Listener listener) {
listeners.add(listener);
}
}
public class Act1 extends Activity implements Network.Listener {
#Override
public void onCreate(Bundle b) {
Network.getInstance().addListener(this);
}
public void event(String msg) {
// do nothing
}
}
public class Act2 extends Activity implements Network.Listener {
#Override
public void onCreate(Bundle b) {
Network.getInstance().addListener(new Network.Listener() {
public void event(String msg) {
// do nothing
}
);
}
public void event(String msg) {
}
}
Will either of the Activities leak once a user moves on to another Activity? Will either Activity be prevented from getting GCed? Are there any problems with the Listener implementation above?
Your example has some mistakes.
For example you never instantiate Network and every time you instantiate it, the static field will reference another instance. A more proper way to write Network would be the following
public class Network {
private static final Network INSTANCE = new Network();
private Network() {/*empty*/}
public static Network get() {
return INSTANCE;
}
//rest of the code ommitted
}
However, if you are not removing the Activity instances from the Network singleton (that static field instance in the Network class - assuming you initialize it at some point), then that instance will always have a reference to your Activity objects that were added to it's ArrayList. This way they cannot be garbage collected.