0

First of all, I searched a lot but I dont know what i'm doing it wrong!.

I have MainActivity which has a Listview and add button, the a button take you to addnewActivity which you can enter new items to the listview in the MainActivity and take you back to it.

Now, the problem is, every time I add new item I get new it in a new instance of the MainActivity which doesnt contain the update of the open before it! (First new item wont be visible the second time I add new item, each in different one! ).

All I need is, One MainActivity every time I add to it, so no replication (going back from it should close the app) and the data always move from the older to the newest.

  • I use startActivity(intent); to more between them
  • using back button from the addnewActivity should back to latest MainActivity.

I believe this is how normal app should behave!. Thanks

update:

my add new item button

private void additembutt() {
Intent intent = new Intent(this, add.class);
int idx = Items.size(); //this is the List<items> (to get the the last postion)
intent.putExtra(ID_X, idx); //newPosition

startActivity(intent);
//startActivityForResult(intent, 1); //not using this anymore, didnt work
//finish();
}

and on the add.class

Intent intent = getIntent();

idx = intent.getIntExtra(MainActivity.ID_X, -1); //take the id

Intent intentback = new Intent(add.this, MainActivity.class);

//read the user inputs and put them in msges like this as well
intentback.putExtra(ID_X, msg_idx); //Position
intentback.putExtra(ID_MESSAGE, msg_name);

startActivity(intentback);
finish();
et3rnal
  • 322
  • 4
  • 17

2 Answers2

0

Instead of using startActivity(mainIntent) you should be using startActivityForResult()

Referenced from another SO question

Community
  • 1
  • 1
Sudhir Mishra
  • 578
  • 2
  • 16
  • I'v tried that, the mainActivity stopped getting data from the second one! maybe because i used .putExtra to send them together ?? – et3rnal Apr 06 '14 at 04:47
  • You see, the `finish()` which you have after the line `startActivityForResult()` destroys the `MainActivity` bcoz of which the `MainActivity` stops getting data from the second one. – Sudhir Mishra Apr 06 '14 at 04:58
  • I think I fixed it :), I didnt know that I can receive the data im sending inside onActivityResult :) thanks – et3rnal Apr 06 '14 at 05:03
0

Try removing

startActivity(intentback);

And see if you get desired result. For more information refere to this

Tech Agent
  • 657
  • 5
  • 12