Stop asynctask from other activity - android

I creating an app using asynctask to download file from server. It has several activities. I start one asynctask from activity A and I want stop it from activity B. How can I archieve it ???

It is possible to call a previous activity's public methods by using typecasting of the getParentActivity() method on your activity B :
((PreviousActivity)getParentActivity()).somePublicMethod();
This only works if you opened Activity B from Activity A. If you want to be able to call that method from all activities, try creating a static class and save an instance of Activity A. This way, wherever you are on the app, the method can be called. Be also wary of null values when doing this.
This is the sample of the static class.
public static class Constants{
public static ActivityA activityAInstance;
}
When you open ActivityA (onCreate method) save it's instance:
Constants.activityAInstance = ActivityA.this;
This part saves the instance of ActivityA to your static class. So whereEver you are on the app, you can access that instance and call it's public methods:
e.g. the app is on ActivityZ all you have to do is import that static class and call ActivityA instance:
Constants.activityAInstance.somePublicMethodToStopTheAsyncTask();

In my opinion , you can set a boolean flag in activity A ,and when you run the asynctask , you should check this flag ever time,if you want to stop ,you just change the flag , so you could change this flag in activity B to stop the asynctask.
you should try this.

I think you can use LocalBroadcastIntent to send a message from activity B to activity A. In method receiver of A, you can call Asynctask.cancel(true). And you should place if(isCanceled()){
break;
}
in your asynctask's loop to stop the task when you call Asynctask.cancel(true).

Related

Android prevent restart activity after startActivity

I have a problem with startActivity() methods.
Problem Structure
Click Link (example, http://google.com) in some TextView, using LinkMovementMethod
Callback into LinkCatcher class (because some TextView exist almost all activity, so i using outer class)
Post event to MainActivity using Otto (because it prevent startActivity outside of activity problem.)
on MainActivity, intent to Activity B without any Flag, just one extra (link)
When Activity B created, Activity A will destroy and call onCreate(Bundle savedInstanceState) (i insert some Log.e to my Code, definitely it re-call Log code)
Tried Methods
Insert android:launchMode="singleInstance" in MainActivity, Activity B
Insert android:configChanges="orientation|screenSize|keyboardHidden" in MainActivity (it means, orientation is not problem. i already implement onSaveInstanceState in all activity of my Application)
3.remove Step 3. instead of Post event to MainActivity, call startActiivty() in LinkCatcher Class
I tried all methods which i try it. and i don't know why activity is destroy and re-call onCreate (not onResume) even i don't use any finish() methods.
Because that's how Android is designed- at any moment an Activity not currently on screen can be killed. You can't prevent it. What you can do is account for it- you can save off any necessary data in onSaveInstanceState, and restore it in onCreate from the Bundle or in onRestoreInstanceState.

Invoking caller activity from callee activity

I have many activities ( say activity A, B, C) calling a single activity ( say activity D) for a result (startActivityForResult(..)).
But want to do something according to the caller activity in the callee activity ( activity D) before the callee sets result and finishes.
The task I want to do needs to use data variables of the calling activity, so now I am planning to pass a callback class similar to this:
public interface myCallBack implements Seriabizable{
public void myMethod();
}
from each caller to the callee on launching the activity;
intent.putExtra(CALL_BACK_OBJECT_KEY, new myCallBack(){
#Override
public void myMethod(){
//code that uses instance variables of caller class
}
});
of course the callback is seriabizable.
myCallback = getIntent().getSerializableExtra(CALL_BACK_OBJECT_KEY);
Now the callee activity ( activity D) will do the task like this:
myCallback.myMethod();
But is this the right way of doing it? Is it good to pass callback class across activities? What other options are available?
Or is there a way to call methods of the caller activity directly? In this case I will force the caller activities to implement MyCallBack interface.
NB:
The reason I dont want to pass the data needed from caller to callee is, the data needed by the task is completely different for each caller activity and also some time big. So I dont to mess the callee activity with those data variables.
What you suggest may work. However it looks complicated and not a good practice.
If you just want to pass data accross activity, why don't you use intent.putExtra with the data you want to pass accross activities ?
If the data you want to pass are too large, or too complex, to be pass like that, maybe you should implement your application another way. You could for instance use a Service. You wil have a three services (one for each activity, or use case). These services will implement a common interface or herit from the same class. Your calling actvity (A, B or C) will start the appropriate service and the service will store the datas. When you start the activity D, this activity will bind with the service. Then she will be able to fully access all datas or objects from the service.

Cannot close Activity from AlertDialog called in PostExecute

I have a couple of Activities both of which send files up to the internet.
I would like to put the posting procedure into a separate class file so that the same procedure is called for each activity.
I pass the context of the calling activity to the main AsyncTask method which, in turn calls preExecute, doInbackground, Postexecute.
I have an AlertDialog, built using the context passed to the AsyncTask, in the postexecute method that shows the message returned by the server, with an OnClickListener to dismiss it. When the button is clicked the alertdialog is closed but the underlying activity screen is not.
I've tried several different combinations of finish() including:-
dialog.cancel();
finish();
and
dialog.cancel();
MyActivity ma = new MyActivity();
ma.this.finish();
nothing, though, will close the Activity.
What am I doing wrong and what do I need to do to get it to close?
So far the only way I can get it to work is to embed the exact same code in both Activities which seems to me to be both inefficient and susceptible to error.
Any help would be very welcome.
I pass the context of the calling activity to the main AsyncTask
instead of only passing context of Activity you will need to pass Activity instance to class in which extending AsyncTask class because finish method is from Activity class instead of from Context .so try it as:
public class Networkasynctask extends AsyncTask<...>{
Activity activity;
Context context;
public Networkasynctask(Context context,Activity activity,...){
this.activity=activity;
.....
}
....
}
and pass Activity instance as from Activity:
Networkasynctask networktaskobj=new Networkasynctask(this);
now use Networkasynctask.this.activity.finish() for closing Activity from non Activity class

Need Suggestion to do a task when Android Boots-Up

I have written an activity A, when users press a button, it will do MyConfig.doSomething() where MyConfig is simple class with activity A passed to it.
public class A extends PreferenceActivity {
private MyConfig mMyConfig;
/* pseudo code, when button clicked, call */
mMyConfig.doSomething();
}
In mMyConfig, it accesses SharedPreferences for some configuration. Thus, I can do this to pass the activity to mMyConfig for calling getSharedPreferences().
mMyConfig = new MyConfig ( this );
Here comes my request:
I want to do something that MyConfig.doSomething() already does, but except when users click some button to invoke it, I want to invoke it when Android Boots-Up.
I can write another class to extend BroadcastReceiver and then starts activity A by calling startActivity(A.class), and then in A, do some tricks to make mMyConfig.doSomething() happen. It works but the Application will be shown on screen when Android Boots-Up.
I want to make mMyConfig.doSomething() happen implicitly without letting users be aware of it. I suppose two possible solutions but I don't know how to do it.
Solution A:
Write a class that extends BroadcastReceiver, start a service (instead of activity A) that reads the SharedPreferences of A and create MyConfig object to do doSomething(). However, I don't if this can work if activity itself is never launched and how could I do this (read SharedPreferences from a service)?
Solution B:
Write a class that extends BroadcastReceiver, start activity A without showing it, put it to activity stack by calling startActivity(A.class) in onReceive. Is this possible?
Instead of Activity, which are meant to be visible to the user, you can make your BoardcastReceiver to start a Service instead. It is meant to perform tasks in the background without disturbing the user. The official guide is a nice place to start with.
Edited:
To access the SharedPreference of your application, simply call this line inside your service:
SharedPreferences pref = PreferenceManager.getSharedPreferences();

Call a member function from an activity, without starting that activity

In my application, there are two activity classes. Suppose A and B. Activity B has a member function name myfun(), Is it possible to call myfun of activity B from activity A with starting activity B. If yes, please provide solution
thanks
You can initiate a object of your ActivityB, and than run the member function. (Use it like an normal object)
E.g
ActivityB act = new ActivityB();
act.yourMethod();
(the onCreate will not run when using the "new" keyword, only the constructor of ActivityB.)
Btw. If the method is not depending on any special state or member variables of the ActivityB class, I sugges you move/refactor the method to another Util class or something like that.
you can create for sure, but you won't able to use that object to start activity, instead you can create a static method, or variable, and can use directly.
public static myFunction(int parameter)
{
// Your code
}
and call this method by
YOurActivtiyClass.myFunction(parameter);

Categories

Resources