1

I want MainActivity call a SecondActivity and the SecondActivity return a List of Strings.

Every answer that I have read explains how to pass data from MainActivity to SecondActivity.

I have created an Activity that calculate all possible IPs in internal network and keep them to a List. I want to pass this List to MainActivity. Could you suggest me some links or code to solve my problem? I am totally new at Android Studio but I have to do it. Here the snippet code from MainActivity

 b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    boolean check_conn = false;
                    check_conn = checkInternetConnection();//check if you are on internet

                    if (check_conn){
                        //we have connection
                        // find the all the possible IPs
                        Intent intent = new Intent(MainActivity.this, rangeIP_test.class);
                        startActivity(intent);
                    }
                    else {
                        // do something annoying
                        Toast.makeText(MainActivity.this,
                                "We need Internet Connection!", Toast.LENGTH_LONG).show();
                    }
                }
            });

The rangeIP_test Activity that calculate IPs

WifiManager my_wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String subnet = getSubnetAddress(my_wifi.getDhcpInfo().gateway);
IPs = findIP(subnet);

public List<String> findIP(String subnet){
        List<String> all_IPs;// All IPs on network
        all_IPs = new ArrayList<>();

        for (int i=2; i<255; i++){
            String host = subnet + "." + i;

            all_IPs.add(host);
        }
        return all_IPs;
    }
Md. Zakir Hossain
  • 1,082
  • 11
  • 24
0sunday
  • 15
  • 7
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Grimthorr Oct 12 '18 at 11:52
  • Possible duplicate of [How to put a List in intent](https://stackoverflow.com/questions/6541088/how-to-put-a-list-in-intent) – Uday Ramjiyani Oct 12 '18 at 11:58

3 Answers3

3

Start the activity, rangeIP_test, for a result(i.e., list of strings of hosts) like this:

 Intent intent = new Intent(MainActivity.this, rangeIP_test.class);
 startActivityForResult(intent,RC_LIST_STRING);

Define RC_LIST_STRING as a field like this in MainActivity.java:

 private static final int RC_LIST_STRING = 10001;

Override onActivityResult() in MainActivity.java like this:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == RC_LIST_STRING) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
               // get the list of strings here
               ArrayList<String> myHosts = (ArrayList<String>) data.getSerializableExtra("myHosts"); // key must be matching 
               // do operations on the list
            }
        }
    }

In rangeIP_test.java, after successfully getting all the hosts and adding them into a list, you can pass the list back to whichever activity(here, MainActivity) started the current activity(here, rangeIP_test) like this:

Intent intent = new Intent();
intent.putExtra("myHosts", (Serializable) IPs);
setResult(RESULT_OK, intent);
finish();

Now, that you've finished rangeIP_test activity, you're back to MainActivity and since you've overriden onActivityResult(), you'll get the list of hosts over there.

Bear in mind that, in this example, you're converting a list of hosts into a serialized type and passing it to previous activity. Since String and ArrayList already implement Serializable interface, you won't get any unable to marshal value error. However, while passing your model objects around activities, you need to first make the model class implement Serializable interface and then proceed further as above.

The better approach is to convert model classes into Parcelable and then pass the data among activities. Please refer to this link for more info.

0sunday
  • 15
  • 7
Jay
  • 2,852
  • 1
  • 15
  • 28
  • 1
    First of all, thanks for the detailed answer and your time! Thank you so much @Jay I wasted too much time to find the answer. Thank you! – 0sunday Oct 12 '18 at 13:13
  • I have to mention that in command intent.putExtra("myHosts", all_IPs); I got an error and I try the four solution that AS suggests. It work when I make the list Serializable. – 0sunday Oct 12 '18 at 13:21
  • @OrfeasKazepis could you edit my answer to remove the above error? So that others won't get that error again. – Jay Oct 16 '18 at 05:27
1

You can directly pass list into intent extras

something like this

Intent intent = getIntent();  
intent.putStringArrayListExtra("test", (ArrayList<String>) all_IPs);

in another activity, you can receive it like below

ArrayList<String> test = getIntent().getStringArrayListExtra("test");
Uday Ramjiyani
  • 1,407
  • 9
  • 22
1

Using startActivityForResult and onActivityResult may works.

call rangeIP_test Activity by startActivityForResult() and return your list by setResult(). Don't forget to finish() when jobs done.

miririt
  • 11
  • 1