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.
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.