Reputation: 453
This is ViewActivity
public class ViewActivity extends AppCompatActivity {
private ListView lvPerson;
private PersonListAdapter adapter;
public List<Person> mPersonList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
lvPerson = (ListView)findViewById(R.id.listView);
//Add new person
Intent intent = getIntent();
if(intent.hasExtra("add")){
String name = intent.getStringExtra("name");
String email = intent.getStringExtra("email");
String address = intent.getStringExtra("address");
mPersonList.add(new Person(name, email, address));
Toast.makeText(getApplicationContext(), "Data berhasil ditambahkan", Toast.LENGTH_SHORT).show();
}
//Init adapter
adapter = new PersonListAdapter(getApplicationContext(), mPersonList);
lvPerson.setAdapter(adapter);
}
}
This is InputActivity
public class InputActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
}
public void saveButtonClicked(View view){
EditText name = (EditText) findViewById(R.id.editTextName);
EditText email = (EditText) findViewById(R.id.editTextEmail);
EditText address = (EditText) findViewById(R.id.editTextAddress);
String strName = name.getText().toString();
String strEmail = email.getText().toString();
String strAddress = address.getText().toString();
Intent intent = new Intent(this, ViewActivity.class);
intent.putExtra("add", true);
intent.putExtra("name", strName);
intent.putExtra("email", strEmail);
intent.putExtra("address", strAddress);
startActivity(intent);
}
}
Show my flow is when save button clicked in InputActivity, it will run "add person" code in ViewActivity and add data from form to ArrayList.
That's work, but when i changing activity using intent and i back to ViewActivity, ArrayList data gone.
What i want is to keep all value that i've already input in InputActivity.
Hope you understand what i mean :)
Upvotes: 1
Views: 6172
Reputation: 10910
There are some good answers here, but since you only want it to persist during one app session I'll add another approach, a singleton to hold the data:
class DataHolder {
final List<Person> people = new ArrayList<>;
private DataHolder() {}
static DataHolder getInstance() {
if( instance == null ) {
instance = new DataHolder();
}
return instance;
}
private static DataHolder instance;
}
In either activity (or any place in your app), you can access the person list with
List<Person> ppl = DataHolder.getInstance().people;
For example, you would replace
public List<Person> mPersonList = new ArrayList<>();
with
// not sure why that was public...
private List<Person> mPersonList = DataHolder.getInstance().people;
It will only last for your current app session, but if you wanted to save them in the future you could add a backend database to the DataHolder so that every time you added a person it was automatically saved somewhere.
Upvotes: 4
Reputation: 31
Override onSaveInstanceState(Bundle) in InputActivity to save whatever data you want then override onRestoreInstanceState(Bundle) in the same activity to get the values back. Using Bundle, you can store pretty much any data that you want. I'd recommend something like this:
public class ViewActivity extends AppCompatActivity {
private ListView lvPerson;
private PersonListAdapter adapter;
public List<Person> mPersonList = new ArrayList<>();
@Override
protected void onSaveInstanceState (Bundle outState) {
outState.putStringArrayList("mPersonList", mPersonList); //I assume you want to mPersonList
}
@Override
protected void onRestoreInstanceState (Bundle savedInstanceState) {
this.mPersonList = savedInstanceState.getStringArrayList("mPersonList"); //on coming back retrieve all values using key
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
lvPerson = (ListView)findViewById(R.id.listView);
//Add new person
Intent intent = getIntent();
if(intent.hasExtra("add")){
String name = intent.getStringExtra("name");
String email = intent.getStringExtra("email");
String address = intent.getStringExtra("address");
mPersonList.add(new Person(name, email, address));
Toast.makeText(getApplicationContext(), "Data berhasil ditambahkan", Toast.LENGTH_SHORT).show();
}
//Init adapter
adapter = new PersonListAdapter(getApplicationContext(), mPersonList);
lvPerson.setAdapter(adapter);
}
}
Upvotes: 3
Reputation: 3234
What you're doing is actually very very bad, since you're just creating Activity after Activity for each Person you want to add and create. It wastes memory and power.
Instead, what you should look for is simply to return the results from InputActivity back to ViewActivity.
In your ViewActivity, don't call startActivity(intent)
, instead use startActivityForResult(intent, REQUEST_CODE)
. Then override onActivityResult()
so you can handle the results that are returned back to ViewActivity.
In InputActivity, don't call startActivity()
instead, return the intent back to the ViewActivity.
An example of this is like so:
ViewActivity
public class ViewActivity extends AppCompatActivity {
private ListView lvPerson;
private PersonListAdapter adapter;
public List<Person> mPersonList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
lvPerson = (ListView)findViewById(R.id.listView);
//Init adapter
adapter = new PersonListAdapter(getApplicationContext(), mPersonList);
lvPerson.setAdapter(adapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK){
String name = data.getStringExtra("name");
String email = data.getStringExtra("email");
String address = data.getStringExtra("address");
mPersonList.add(new Person(name, email, address));
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Data berhasil ditambahkan", Toast.LENGTH_SHORT).show();
}
}
}
InputActivity
public class InputActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
}
public void saveButtonClicked(View view){
EditText name = (EditText) findViewById(R.id.editTextName);
EditText email = (EditText) findViewById(R.id.editTextEmail);
EditText address = (EditText) findViewById(R.id.editTextAddress);
String strName = name.getText().toString();
String strEmail = email.getText().toString();
String strAddress = address.getText().toString();
Intent intent = new Intent();
intent.putExtra("add", true);
intent.putExtra("name", strName);
intent.putExtra("email", strEmail);
intent.putExtra("address", strAddress);
setResult(RESULT_OK, intent);
finish();
}
}
Doing it this way avoids recreating ViewActivity again so you don't have to recreate all the views and repopulate the lists. You simply reuse the existing ViewActivity and add the results to the existing list.
Upvotes: 2
Reputation: 11
If i understand you correctly, your flow is the following: 1.) ViewActivity -> Intent -> InputActivity 2.) InputActivity -> Intent -> ViewActivity
If that is the case, you should read more about the "Activity Lifecycle" in Android: https://developer.android.com/guide/components/activities/activity-lifecycle
In short: onCreate only gets called at the creation of the ViewActivity. If you go from there to the InputActivity and then back -> ViewActivity already exists and therefore oncreate does not get called.
One solution which comes to my mind for your case is startActivityForResult. Here an example on how to use it: How to manage `startActivityForResult` on Android?
Another general tip, as it seems that you are fairly new to android development: Use breakpoints and Log (https://developer.android.com/reference/android/util/Log) heavily to get a better feeling where and when things are happening. And do not only use tutorials to start development in Android. It does not hurt to read some beginners guide/book that explains some android specifics in more detail (like the Activity-Lifecycle)
Upvotes: 1
Reputation: 995
You can serialize it as a string, using Gson/Jackson/or whatever, and then save it with [SharedPreferences][1]
. And then in the new activity you just load it back from SharedPreferences
.
Upvotes: 0