Accessing Another Activity's Textview For Appending Text In Different Activities
Solution 1:
Use SharedPreferences as shown here to save your appended string after each activity and just use that in the last one as textview.setText(pref.getString("key",null));
If you dont understand how to create/use SharedPreferences leave a comment and ill be happy to help
Update: in every activity declare-
SharedPreferences pref;
SharedPreferences.Editor editor;
then inside onCreate()-
pref = getSharedPreferences("name", MODE_PRIVATE);editor = pref.edit();
you can use any string instead of "name" above. Its the name of your SharedPreference file.
now to save string -
editor.putString("myString", "some string");
editor.apply();
to get string -
String s=pref.getString("MyString",null);
Just getString in 2nd activity onwards ->append using '+' -> save it again using editor.put. That should do it :) google SharedPreferences for further info
Solution 2:
You can put your text in intents which you use to start your activity.
Intenti=newIntent(FirstScreen.this, SecondScreen.class);
StringstrName="abc";
i.putExtra("STRING_I_NEED", strName);
Then in the next activity you can get the text by
Bundleextras= getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
Solution 3:
You can use intent.putExtra() method like
Intent intent = newIntent(this, your_next_activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("src", textViewResult.getText().toString() );
startActivity(intent);
then in you next activity in oncreate() method use below to retrieve
Intenti= getIntent();
Strings= i.getExtras().getString("src");
Solution 4:
In your case, the easiest way is: using Application
:
//create class App that extends android.app.ApplicationpublicclassAppextendsApplication {
publicStringyourTextToShow="";
@OverridepublicvoidonCreate() {
super.onCreate();
}
}
Modify your AndroidManifest.xml 's <application>
tag to have the attribute android:name="your.package.name.App".
From then on, whenever you want to change/access your text to from any Activity
, just call: ((App)getApplication()).yourTextToShow = "textyouwant";
. In your case, you need to reset your TextView
in onResume
of your activity.
P/s: dont try to use static TextView
. It's the worst practice. It will create memory leaks to your app.
Post a Comment for "Accessing Another Activity's Textview For Appending Text In Different Activities"