-2

Is there a way for me to keep the entered text in the text view after leaving the activity?

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ClassDatabase extends AppCompatActivity {

TextView students;
TextView averages;
String studentNames;
String studentAvgs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_class_database);

    students = findViewById(R.id.data);
    averages = findViewById(R.id.avgs);

displays the intent;

    displayIntent();

back to previous activity where u enter new text

    BacktoAdd();
}



private void BacktoAdd(){ 

    Button Back2Add = (Button) findViewById(R.id.backtoadd);
    Back2Add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            startActivity(new Intent(ClassDatabase.this, Add.class));
        }
    });
}

adds new text to text view

private void displayIntent(){

    studentNames = getIntent().getExtras().getString("class");
    studentAvgs = getIntent().getExtras().getString("avg");

    students.append(studentNames);
    students.append(" \n");
    averages.append(studentAvgs);
    averages.append(" \n");


}

}

Currently it gets the text from the previous activity and appends the text to the textview but when I go back to the previous activity and repeat the actions the previous text wasn't saved

Panda
  • 1
  • 1
  • 2
    Please edit your question to include sample code as a [mcve] – OneCricketeer Jul 08 '18 at 14:31
  • 1
    Possible duplicate of [How to save data in an android app](https://stackoverflow.com/questions/10962344/how-to-save-data-in-an-android-app) – Zoe Jul 08 '18 at 14:39
  • Possible duplicate of [Save user input to SharedPrefernces and set Value to EdiText](https://stackoverflow.com/questions/45931720/save-user-input-to-sharedprefernces-and-set-value-to-editext) – Nuwan Alawatta Jul 08 '18 at 14:45

1 Answers1

-1

You can use SharedPreferences https://developer.android.com/training/data-storage/shared-preferences

MonStar
  • 102
  • 1
  • 2
  • 14
  • 3
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – Zoe Jul 08 '18 at 14:38
  • I see that help on this site is not welcome. There is no desire to help someone, when you are given a minus for it – MonStar Jul 08 '18 at 14:47
  • content on this site is required to follow the rules defined by SE. One of them being that link-only answers aren't allowed; they'll be deleted. Downvotes are used when a post isn't useful, and obviously, there's no requirement to downvote. Link-only answers are low-quality by the SE definition. It has nothing to do with it being welcome on the site or not; it has to do with the defined rules. If that link dies, the content of it (and through that, this answer) would be useless. Please read [how to answer](https://stackoverflow.com/help/how-to-answer) – Zoe Jul 08 '18 at 14:52