1

First of all thanks for reading and spending your time for solving this problem. I have some Acivities which each contain a button for going to next. The last One contains a TextView which i want to append() a text each activity has finished its job. I used static to access the TextView but because the activity which contain textView is the last one running i get NullException. I'll appreciate any advice. thanks ;)

the last activity:

public class FinishActivity extends Activity {

    static TextView textViewResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finish_layout);
        textViewResult = (TextView)findViewById(R.id.textViewResult);

    }
}
mazhar
  • 180
  • 1
  • 11

4 Answers4

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

Shreyans
  • 1,043
  • 2
  • 14
  • 25
  • well i don't know, and im listening cearfully :) – mazhar Mar 18 '16 at 10:09
  • i have a problem though. when i call the same activity (for some long story reason) couple of times, it only saves the last one! – mazhar Mar 18 '16 at 11:19
  • if you save "string1" first then save getString("MyString")+"string2" next time and so on. the '+' operator is used to append strings. does that solve your problem? – Shreyans Mar 18 '16 at 11:22
  • i used `editor.putString("myStr", pref.getString("myStr", null) + "\n" + "some str");` and seems to have some problems but it works! thanks man. i wish you best – mazhar Mar 18 '16 at 11:34
  • hey no problem :) though i would suggest you watch some video tutorials to get basic idea about all the different parts of android. check out thenewboston on youtube or maybe do a free Udacity course. And dont forget to accept my answer as correct and upvote :) – Shreyans Mar 18 '16 at 11:38
  • sure thanks :) my points are too low to vote! but as soon as i improved my account ill upvote :) have a nice day ;) – mazhar Mar 18 '16 at 11:45
  • yeah but you can click on the 'tick' to choose this as the correct answer :) – Shreyans Mar 18 '16 at 11:50
  • thanks i needed that :) and dont worry you will learn! i can see you are enthusiastic :D – Shreyans Mar 18 '16 at 11:53
  • 1
    don't thank me. you earned it :) and thanks for your kindness. – mazhar Mar 18 '16 at 11:56
0

You can put your text in intents which you use to start your activity.

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String strName = "abc";
i.putExtra("STRING_I_NEED", strName);

Then in the next activity you can get the text by

Bundle extras = getIntent().getExtras();
if(extras == null) {
    newString= null;
} else {
    newString= extras.getString("STRING_I_NEED");
}
Passiondroid
  • 1,573
  • 1
  • 16
  • 28
  • HI, thanks for your solution, yeah i know how to send Extra by intent, but my Intents are used to run the next Activity while the text i want to append is in the last Activity which runs at last. – mazhar Mar 18 '16 at 09:40
  • You mean the text on which you want to append is on the first activity when you open the app. In this case you can use startActivityForResult. Check the developer site - http://developer.android.com/training/basics/intents/result.html – Passiondroid Mar 18 '16 at 09:44
0

You can use intent.putExtra() method like

   Intent intent = new Intent(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

    Intent i = getIntent();
    String s = i.getExtras().getString("src");
Thush-Fdo
  • 506
  • 1
  • 9
  • 28
0

In your case, the easiest way is: using Application:

//create class App that extends android.app.Application
public class App extends Application {
     public String yourTextToShow = "";    

     @Override 
     public void onCreate() {
         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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86