0

So I have something like this:

protected void populateTransactionList() {
    myTransactions.add(new Transaction(78,98,"hello"));
}

Now I want this to happen only if there is a new intent from another activity (that carries some data to replace the (78,98,"hello")). Since I am not very experienced with coding could somebody please help we with this little problem? (The Intent is not my problem but the population of the ArrayList. I did not find a thread that could help me with this.) thank you.

Lukas Schröder
  • 344
  • 1
  • 2
  • 16
  • 1
    Possible duplicate of [How to send an object from one Android Activity to another using Intents?](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – Jozef Dochan Feb 08 '17 at 19:58
  • @JozefDochan no I've seen this one but that is not my problem. I want to add an Item to the ArrayList every time there is a new Intent – Lukas Schröder Feb 08 '17 at 20:04
  • if you are creating another activity and then awaiting result try using [startActivityForResult()](https://developer.android.com/training/basics/intents/result.html) – Jozef Dochan Feb 08 '17 at 21:07

1 Answers1

-1

You want to override the activity method onNewIntent(Intent intent)

@Override
public void onNewIntent(Intent intent) {
    // call intent.getExtra(..) method to get your stored transaction value(s)

    // call myTransactions.add(...) with the values from the intent
}
kira_codes
  • 1,457
  • 13
  • 38
  • I have tried this but it did not work. Nothing was added to the ArrayList. – Lukas Schröder Feb 08 '17 at 20:09
  • If you've tried it can you post the code that you tried? Things to check: How are you putting the values in the intent? how are you attempting to access them? – kira_codes Feb 08 '17 at 20:18
  • This is how I am accessing the Intent. I know that it is sent properly because I did use it elsewhere... `protected void onNewIntent(Intent depositIntent) { super.onNewIntent(depositIntent); Transaction deposit = depositIntent.getParcelableExtra("data"); Float newVal = deposit.getValue(); String newDes = deposit.getDescription(); Integer newDat = deposit.getTransaction_Date(); myTransactions.add(new Transaction(newVal,newDat,newDes)); }` – Lukas Schröder Feb 08 '17 at 20:42
  • have you set a breakpoint and checked the values with the debugger to see if it was stored in the intent correctly and if your parcelable implementation is working correctly? – kira_codes Feb 08 '17 at 21:31
  • are you sure that you aren't re-initializing the array list anywhere after you have added the new values? Can you post the full code? – kira_codes Feb 09 '17 at 19:13
  • here it is: http://stackoverflow.com/questions/41666805/array-list-items-replace-old-ones-instead-of-being-added (I posted this question again because maybe it was a bit too specific in the linked question) – Lukas Schröder Feb 10 '17 at 12:23
  • If you're using the base transaction class it isn't parcelable, so I think the problem is writing it to the intent and reading it back properly. I would extend it and implement parcelable. The source can be found here: https://developer.android.com/samples/AsymmetricFingerprintDialog/src/com.example.android.asymmetricfingerprintdialog/server/Transaction.html – kira_codes Feb 10 '17 at 17:55
  • You could also store each of the transaction values as individual extras, fetch them individually in onNewIntent, and then create your new intent there. – kira_codes Feb 10 '17 at 18:02