0

I am quite new to Android and facing a problem.

I have a class A from where i would like to call another activity. I have found some post saying that there is no way to pause the calling Activity and wait for the result.

    public class A extends AppCompatActivity {
    [...]
    private List<String> list = new ArrayList<String>();

    private void doSomething() {
        list.add("a");
        list.add("b");
        for(String tmp:list) {
            Intent intent = new Intent(this, OtherActivity.class);
            intent.putStringExtra("TAG", tmp);
            startActivityForResult(intent, 1);
        }
    }

This is not a complete example but basically the problem I am facing.

I have a loop and try to open another activity. The loop does not stop when i start the "OtherActivity". The first thing i see is the OtherActivity for the last element of the list (here String "b"). When i finish this Activity i see the OtherActivity with String "a" in wrong order.

I considered a callback for this, but i am not sure how to implement it because the callback handler wouldn't be within the loop.

Again I am not sure if a callback would be a good idea because many people say i should not Pause the "calling" activity for the sub activity.

Entrail
  • 1
  • 1
  • 4
  • Are you trying to send data from one activity to the other? – Denny Apr 25 '17 at 07:08
  • Jeah i forgot to add that line. But this is not my main Problem. The problem is that the loop creates as much new Activities as there are Objects in the List as Abdul Kawee said – Entrail Apr 25 '17 at 07:11

2 Answers2

1

You are doing it totally wrong, if you want to send data to other activity and do some work then get the result , i would prefer that send the whole data as a list , do the work and then get the data from that activity , you shouldn't be doing it in a loop. Either pass it is as intent or save it in database then retrieve from database.

Johnett Mathew
  • 307
  • 1
  • 10
  • 20
Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26
  • Thanks for your reply :) But my OtherActivity just works with one passed Object not with the list. – Entrail Apr 25 '17 at 07:06
  • Thats not an issue, change your other activity as per your need. The way you code is working it will call the activities without waiting for results , meaning many activities will be created. – Abdul Kawee Apr 25 '17 at 07:08
0

If you want to pass the whole list of string to the other activity I suggest you do this

You can pass an ArrayList the same way, if the E type is Serializable.

You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval.

Example:

ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList); 

In the other Activity:

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");

Please note that serialization can cause performance issues: it takes time, and a lot of objects will be allocated (and thus, have to be garbage collected)

Source: Passing a List from one Activity to another

Or as Abdul suggested, save the data to a database and retrieve it from there in the other activity

Community
  • 1
  • 1
Denny
  • 1,766
  • 3
  • 17
  • 37