mazhar
mazhar

Reputation: 190

accessing another activity's textView for appending text in different activities

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);

    }
}

Upvotes: 1

Views: 518

Answers (4)

Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8200

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.

Upvotes: 0

Shreyans
Shreyans

Reputation: 1061

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

Upvotes: 1

Thush-Fdo
Thush-Fdo

Reputation: 514

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");

Upvotes: 0

Passiondroid
Passiondroid

Reputation: 1589

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");
}

Upvotes: 0

Related Questions