Search code examples
androidandroid-intentandroid-bundle

Return list from a Activity to Main Activity?


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;
    }

Solution

  • 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.