0

I have an app that for right now, consists of 2 buttons (will later consists of 20+). When I click on a button, it takes me to a new activity that has a list of items I can select. After selecting something and clicking the Back button, it starts a new activity that passes on the item's information (in this case, "orange") and then it assigns the word "orange" to the button that was clicked.

Now when I click on the other button to assign it's information, I lose all of my first button information. What are my options for saving the previous information? Would I have to create an intent for it and keep passing it back and forth between actvities?

At the end, I need to collect all the information that was assigned to both buttons and pass that onto another activity, as this is just the customizing page. Is there a way I can just have the Strings set such that leaving the activity won't delete the String information?

Here's my MainActivity

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        btnValue = extras.getString("btnValue");
        itemValue = extras.getString("itemValue");
    }

    if (btnValue.equals("btn1")){
        btn1.setText(itemValue);
    } else if (btnValue.equals("btn2")) {
        btn2.setText(itemValue);
    }
}

public void onClickBtn1(View v) {
    Intent myIntent = new Intent(this, Main2Activity.class);
    myIntent.putExtra("btn", "btn1");
    startActivity(myIntent);
}

public void onClickBtn2(View v) {
    Intent myIntent = new Intent(this, Main2Activity.class);
    myIntent.putExtra("btn", "btn2");
    startActivity(myIntent);
}

and my 2nd activity

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        btnValue = extras.getString("btn");
    }

    listView = (ListView) findViewById(R.id.list);
    String[] values = new String[] { "apple", "banana", "orange", "cherry"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            itemPosition = position; 
            itemPositionString = String.valueOf(itemPosition);
        }
    });
}

public void onClickBack (View v) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("btnValue", btnValue);
    intent.putExtra("itemValue", itemValue);
    startActivity(intent);
}

enter image description here

enter image description here

enter image description here

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Pam
  • 73
  • 9
  • @MehdiKhademloo sorry, what part was unclear? I just lose all my information when I am back on my main page and click on another button to assign it it's text – Pam Nov 24 '16 at 20:29

3 Answers3

1

You can use the Android lifecycle to manage the activity's state. To save the activity state you need to do your work on the method onSaveInstanceState.

onSaveInstanceState(Bundle bundle)

On restoration you either check the bundle the following methods

onRestoreInstanceState(Bundle bundle)
onCreate(Bundle bundle)

You can find more details here: https://developer.android.com/training/basics/activity-lifecycle/recreating.html

Sonny
  • 186
  • 1
  • 10
0

When you use startActivity it creates new activity. So you lose old information. You need to use startActivityForResult from MainActivity while starting second activity and from the second activity you should use setResult

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK, returnIntent);
finish();

And you handle the result in MainActivity with

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
..
..
}

Here, there is an example How to manage `startActivityForResult` on Android?

Community
  • 1
  • 1
misman
  • 1,347
  • 3
  • 21
  • 39
0

You basically have 3 choices:

  1. Pass all data in the Intent and return any new/changed data in another Intent using startActivityForResult().
  2. Save the data in a static variable somewhere (globals). All activities can then reference the current data and make changes to it that are then seen by all other activities. This is the quick-and-dirty solution which is suitable for small, trivial or "homework" solutions.
  3. Save the data in a persistent storage (a file or an SQLite database). All activities can read all the current data, display it and make changes. After the BACK button is pressed, the underlying Activity should read the current data from the persistent storage to refresh the views.
David Wasser
  • 93,459
  • 16
  • 209
  • 274