I have Activity1 and Activity2. Both can call a class named "fetchData.java". Is there any way inside fetchData.java wherein I can get which activity called it?
Here is my fetchData.java:
public class fetchData extends AsyncTask<String,String,String> {
#Override
protected String doInBackground(String... voids) {
//do something
}
#Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
}
}
Activity1 and Activity2 code:
public class Activity1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Call fetchData.java
fetchData getValues = new fetchData();
getValues.execute();
}
The only way I can see for you to do this is by passing the Activity in the constructor:
public class FetchData extends AsyncTask<String, String, String> { //rename fetchData to FetchData to follow Java coding practices
private Activity activity;
public FetchData(Activity activity) {
this.activity = activity;
}
#Override
protected String doInBackground(String... strings) { //use the proper variable names
//...
}
#Override
protected void onPostExecute(String string) { //use the proper variable names
//use instanceof to check the identity of the activity instance
if (activity instanceof Activity1) {
//whatever
} else if (activity instanceof Activity2) {
//whatever else
}
//super call isn't needed
}
}
And to instantiate it:
FetchData getValues = new FetchData(this);
getValues.execute();
Take note of my comments. I made a few changes to your code to improve readability and conform better to Java's coding standards.
In other cases, you might be able to read the stacktrace and find the calling class, but since you're using an AsyncTask, which runs on another Thread, the stacktrace only goes back to when the Thread was created, so it wouldn't work here.
Related
I've created an interface which holds a reference to an interfaces instantiated from an activity.
This is the interface:
public interface Calback {
void fun();
}
This is the activity which instantiates the calback and binds it to asincktask.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt = findViewById(R.id.helloTxtv);
txt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Calback call = new Calback() {
#Override
public void fun() {
Log.d("tag","text of destroyed activity: "+((TextView)findViewById(R.id.helloTxtv)).getText());
}
};
Worker worker = new Worker(call);
worker.execute();
}
});
}
}
What's strange is that using that calback I can access textview even if the activity was destroyed.
This is the code from asyncktask:
public class Worker extends AsyncTask<Void, Void, Void> {
private final Calback call;
public Worker(Calback call) {
this.call = call;
}
#Override
protected Void doInBackground(Void... voids) {
try {
sleep(5000);
Log.d("tag","done");
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
call.fun();
}
}
To ensure that the activity it's destroyed I've just rotated the screen.(But I've got the same result after starting another activity and finish the current one)
And here is the log result.
PS: I've used Android Studio 3.0
If you are able to access the text of the TextView after the parent Activity has been destroyed, then you have a memory leak.
However, I'm not convinced that is what is going on here. I think it is more likely that either the activity has not been destroyed, or the activity's state was persistent and you are now looking at the state in the new (reincarnated) activity.
Why? Because, it seems that the callback is being called via an onClick listener for the text view. And that can only occur if the specific text view is still visible. It can't be visible if it is a component of a destroyed activity.
I need to set the String value generated by from AsynTask's OnPostExecute() method to Another Activity's Textview for that I have used following two methods, but both methods fails.
Method 1: By using GetterSetter Class
I am fairly new to android. I am making an app in which i stuck to this particular problem. I am running asyncTask from BackgroundTask class and BackgroundTask is running from StringGenerator class. In this AsyncTask I am setting value of a string to the getterSetter class.
My String Generator class -->
public class StringGenerator {
...
new BackgroundTask(context).execute(sb.toString());
Intent intent = new Intent(context.getApplicationContext(),Answer.class);
context.startActivity(intent);
sb.setLength(0);
}
My BackgroundTask Class -->
class BackgroundTask extends AsyncTask<String, Void, String> {
Context context;
GetterSetter getterSetter;
...
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("Answer",s);
getterSetter = new GetterSetter();
getterSetter.setString(s);
}
}
My GetterSetter Class, here log prints correct string.So, the string is set here, I have verified it. -->
class GetterSetter{
private String string;
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
Log.d("S1",string);
Log.d("S2",this.string);
}
}
From this getterSetter class i want to access string and set it onto textview onto another Activity called Answer Activity.
My Answer Activity -->
public class Answer extends AppCompatActivity {
GetterSetter getterSetter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
getterSetter = new GetterSetter();
((TextView)findViewById(R.id.SetSteps)).setText(getterSetter.getString());
}
}
Problem with this method is -->
But the String value set on the textview is empty, so it prints
nothing. Also, i tried to log the string.
Method 2 : By using SharedPreferences,but it always gives me default value which is ""(Empty).
My StringGenerator Class -->
public class StringGenerator {
...
new BackgroundTask(context).execute(sb.toString());
Intent intent = new Intent(context.getApplicationContext(),Answer.class);
context.startActivity(intent);
sb.setLength(0);
}
My BackgroundTask Class -->
class BackgroundTask extends AsyncTask<String, Void, String> {
Context context;
SharedPreferences.Editor editor;
BackgroundTask(Context context) {
this.context = context;
editor = context.getSharedPreferences("SolutionString",context.MODE_PRIVATE).edit();
}
...
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("Answer", s);
editor.putString("Solution",s);
editor.apply();
}
}
My Answer Class -->
public class Answer extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
SharedPreferences sharedPreferences = getSharedPreferences("SolutionString", MODE_PRIVATE);
((TextView)findViewById(R.id.SetSteps)).setText(sharedPreferences.getString("Solution",""));
}
}
Problem with this method is -->
This method will set the correct output string only for first time, after that second time and onwards this method will print default value which is ""(Empty).I tries to debug this method via printing logs and in logs the string values are not empty, i got correct values in logs. So, I think in this method the problem is updation of sharedpreferences value. I have also researched about this on stackoverflow and tried following solution, but nothing works.
SharedPreferences return only default value
getPreferences always returns default value
and many more.
Can anyone tell me how can i access the string inside OnCreate Activity for first method?? or for second method I need to get the updated values for SharedPreferences.
I don't want to create another intent because, i am already running intent from String Generator class.
first of all new GetterSetter() will creates a new object... now lets see how the memory works...
class BackgroundTask extends AsyncTask<String, Void, String> {
Context context;
GetterSetter getterSetter;
...
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("Answer",s);
getterSetter = new GetterSetter(); // assume is stored at address 2000 in the memory
getterSetter.setString(s);
}
}
public class Answer extends AppCompatActivity {
GetterSetter getterSetter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
getterSetter = new GetterSetter();// assume stored at 2008 in the memory , hence the string private String string does not have any value...
((TextView)findViewById(R.id.SetSteps)).setText(getterSetter.getString());
}
}
Now here are the points :-
private String string; if you declare this as private String string = ""; the null pointer exception will not occur.
you should have single GetterSetter getterSetter; intialized getterSetter = new GetterSetter(); only once then the value will persist... if you have both the BackgroundTask and Answer in separate files Answer class wont be able to see BackgroundTask class GetterSetter...
Rather include BackgroundTask with the Activity class and call the textview.setText in onPostExecute...
public class Answer extends AppCompatActivity {
GetterSetter getterSetter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
getterSetter = new GetterSetter();
}
class BackgroundTask extends AsyncTask<String, Void, String> {
Context context;
...
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("Answer",s);
getterSetter.setString(s);
((TextView)findViewById(R.id.SetSteps)).setText(getterSetter.getString());
}
}
}
If you insist on using different files then BackgroundTask should be called first and
class BackgroundTask extends AsyncTask {
Context context;
public static GetterSetter getterSetter; // this will store the object in memory and the value will persist between classes...
...
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("Answer",s);
getterSetter = new GetterSetter();
getterSetter.setString(s);
// the activity Answer should start anywhere after this to get the correct value...
}
}
public class Answer extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
((TextView)findViewById(R.id.SetSteps)).setText(BackgroundTask.getterSetter.getString());
}
}
Hope it helps...
As you are creating a new object so the data in the existing object will be destroyed. So, the simplest way to do the above task is send the object with the intent. You can do this onPostExecute or on click of a button in your first Activity.
Intent intent = new Intent(MainActivity.this, Answer.class);
intent.putExtra("Object", getterSetter);
startActivity(intent);
and your object i.e. GetterSetter must implements Serializable as:
class GetterSetter implements Serializable
and in your another activity i.e. Answer.java you have to receive intent as:
Intent rcv = getIntent();
GetterSetter getterSetter = (GetterSetter) rcv.getSerializableExtra("Object");,
((TextView)findViewById(R.id.SetSteps)).setText(getterSetter.getString());
When I need a callback from Activity B back to Activity A I usually make the reference variable in Activity B 'static'. I realize that if the user rotates the device the Life Cycle methods will remove my reference.
Is this the only drawback and is there a better way to register without a static reference. Is it better to simply put all data in the Application class ? - Thank you.
public class MainActivity extends Activity implements InterfaceMainActivityTwo {
static Main2Activity main2Activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main2Activity = new Main2Activity();
main2Activity.setDataListener(this);
}
#Override
public void getDataMainActivityTwo(String string) {
tvTextData.setText(string);
}
}
public class Main2Activity extends Activity {
static InterfaceMainActivityTwo mGetDataInterface;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void getDataSaveBtn(View v) {
if (mGetDataInterface != null)
mGetDataInterface.getDataMainActivityTwo(fullName);
else
Toast.makeText(this, "IS NULL.INTERFACE NOT INITIALIZED !!!!", Toast.LENGTH_SHORT).show();
}
/////////// interface setup
interface InterfaceMainActivityTwo {
void getDataMainActivityTwo(String string);
}
public void setDataListener(InterfaceMainActivityTwo listener) {
this.mGetDataInterface = listener;
}
}
You should never need a callback between two activities. You're doing something wrong if you do. If you need to pass data from A to B, pass it in the bundle. If you need to pass it back from B to A, use startActivityForResult and pass it in the result. If you need to share data between many activities, it should be held in some globally accessible data structure, either in memory or on disk.
I have an activity with multiple AsyncTask's, but when i press back button, the Activity is reloaded and the AsyncTask's are executed again. what should i do to Back to the previous activity and not reload the activity and asynctask ? please help.
public class LugarActivity extends SherlockActivity {
CargarDatos cargarDatos;
CargarComentarios cargarComentarios;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lugar);
cargarDatos = new CargarDatos();
cargarCometarios = new CargarComentarios();
loadData();
}
public void loadData(){
cargarDatos.execute();
}
public void loadOtherData(){
cargarComentarios.execute();
}
public class CargarDatos extends AsyncTask<Integer, Integer, String>{
#Override
protected String doInBackground(Integer... params) {
// here download data
}
#Override
protected void onPostExecute(String html) {
loadOtherData();
}
}
public class CargarComentarios extends AsyncTask<Integer, Integer, String>{
#Override
protected String doInBackground(Integer... params) {
// here download data
}
}
}
FIXED!
i fixed the problem with Singleton class:
public class DataManager {
private static DataManager instance = null;
protected static boolean isShowingTheView = false;
protected DataManager() { }
public static synchronized DataManager getInstance() {
if (instance == null) {
instance = new DataManager();
}
return instance;
}
}
in the activity i add this code:
DataManager dataManager = new DataManager();
if(!dataManager.isShowingTheView){
loadData();
dataManager.isShowingTheView = true;
}else{
finish();
}
and finally i override the onDestroy() method
#Override
public void onDestroy(){
dataManager.isShowingTheView = false;
super.onDestroy();
}
Remove loadData() from onCreate and call somewhere else.
Use Fragments
http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
A fragment can stay in memory during a configuration change and therefore you can run your asynctask inside itself. You can then query the fragment for any state information you require from your tasks and update your Activity accordingly.
If your Activity is destroyed before the other activity starts, using the back button will call onCreate again, instead of onRestart or onResume.
See here for details.
As Kuffs already mentions, using Fragments is the way to go.
Uglier solution, you could also set a shared preference holding a boolean once your AsyncTask is launched (or on its onPostExecute) so that it won't launch again after checking for that preference on your Activity's onCreate.
I recently needed to use AsyncTask in an activity in my android app. So I made a class inside the activity and extended AsyncTask in that class.
But now, whenever I launch that particular activity, my app immediately crashes. I tried putting the whole onCreate() of the activity in a try, catch block, but no exceptions were raised. The app just crashes immediately when I launch that activity, without any explanation in the LogCat.
This started happening after I added the AsyncTask class mentioned above. Also, the AsyncTask part is NOT executed when the activity is launched, it executes on pressing a button. What could be going wrong? I have added the relevant code below:
public class ListViewA extends Activity{
public void onCreate(Bundle savedInstanceState) {try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
//computation
}
private String[] top() {
new RetreiveFeedTask().execute(ctr,ctz);
return er;
}
public class RetreiveFeedTask extends AsyncTask<Context, Void, String[]> {
String[] q;
protected String[] doInBackground(Context... params) {
//computation
}
protected void onPostExecute() {
er=q;
gff=true;
}
}
EDIT:
I found an error in LogCat which looks important:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.exampleapp/com.example.exampleapp.ListViewA}: java.lang.NullPointerException
Your edit is interesting and suggests that there is a problem in the instantiation of the Activity, in which case I would look at the variable declarations quite carefully. However, in the absence of any of that code (or more onCreate code), or the surrounding lines from the logcat, it's hard to be more specific.
As #Raghunandan says, the way your AsynTask is constructed is incorrect, it should be something like:
public class RetreiveFeedTask extends AsyncTask<Context, Void, String[]> {
String[] q;
#Override
protected String[] doInBackground(Context... params) {
//computation
return result; // result is of type String[]
}
protected void onPostExecute(String[] result) {
// do something with result ...
er=q;
gff=true;
}
}
A secondary point is your top() function, which returns er. I am guessing that you want it to wait until the AsyncTask is complete and then return er, which has been calculated by the AsyncTask. However, what it will do at present is to set the AsyncTask and then immediately return er: it won't wait for the AsyncTask to complete.
To achieve that, you probably need to split the button press action into two phases:
Create and run the AsyncTask
OnPostExecute in the AsyncTask calls the code which currently uses the er returned by top.
public class ListViewA extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
//computation
new RetreiveFeedTask().execute(ctr,ctz);
}
...
public class RetreiveFeedTask extends AsyncTask<Context, Void, String[]>{
#Override
protected String[] doInBackground(Context... fileName) {
//computation
String[] q;
//Know that fileName is an Array
return q
}
protected void onPostExecute(String[] result) {
//do something with the result
}
}
...
}
I have a private class ReadFilesTask extends AsyncTask<String, Void, String> in my program. Under that, I have: protected void onPreExecute(),
protected String doInBackground(String... params), and
protected Void onPostExecute(String result).
The first two have #Override above them. The only one I'm ACTUALLY using though, is doInBackground. I'd first really just try adding #Override to doInBackground(). Next, try changing Context to String.
EDIT:
You are also missing the function to grab an activity. Again, this is simply what I'm doing in my code and a few examples I saw online:
public RetreiveFeedTask(Activity activity) {
this.activity = activity;
}
Something like that.