简体   繁体   中英

Passing ArrayList from SecondActivity to MainActivity Android

my problem is simple, I'm trying to pass an arraylist from my second activity to my mainActivity.

However when my onRestart() in my main activity is called when the user presses back from secondActivity the arraylist seems to be null.

IN MAIN ACTIVITY

@Override public void onRestart() {
        super.onRestart();
        DefaultLocations = (ArrayList<String>) getIntent().getSerializableExtra("updated");

///default locations is null

    }

SECOND ACTIVITY

@Override public void onPause() {

        super.onPause();
        Intent intent = new Intent(ManageLocations.this, MainActivity.class); 
        intent.putExtra("updated",locationsavailable)///size is 2 currently;
        this.setIntent(intent);
}

I've looked around as I know there are many examples but none helped me pass an arraylist BACK to my mainActivity.

All help is appreciated!

In second activity:

ArrayList < String > locationsavailable = new ArrayList < String > ();
locationsavailable.add("location1"); 
locationsavailable.add("location2");

Intent sentIntent = new Intent(SecondActivity.this, FirstActivity.class);
sentIntent.putExtra("string-array", locationsavailable);
startActivity(sentIntent);

In first activity:

Intent intent = getIntent();
String [] DefaultLocations = intent.getStringArrayExtra("string-array");

Edited:

In your case, I suggest use startActivityForResult to start second activity and override onActivityResult in first activity.

The best solution for your situation would be to start the Second Activity using the startActivityForResult method. Here is an example of what you should do:

MainActivity

public class MainActivity extends AppCompatActivity {

private static final int GET_LOCATIONS_REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout layout = findViewById(R.id.mylayout);

    Intent intent = new Intent(this, SecondActivity.class);
    startActivityForResult(intent, GET_LOCATIONS_REQUEST_CODE);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == GET_LOCATIONS_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            ArrayList<String> locations = data.getStringArrayListExtra("result");
            for(String location : locations) {
                System.out.println(String.format("Location from SecondActivity %1$s", location));
            }
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}
}

SecondActivity

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ArrayList<String> locationsavailable = new ArrayList<>();
    locationsavailable.add("location1");
    locationsavailable.add("location1");

    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", locationsavailable);
    setResult(Activity.RESULT_OK, returnIntent);
    finish();
    }
}

If you don't want to start it as an Activity for result, you should override your @OnStart method in the MainActivity.

you just have to write simple code of some lines to solve your problem.

step 1: while sending intent from main activity to second activity use startActivityForResult().

//Main Activity

    private static final int GET_LOCATION_LIST=1; //you can give any value to variable

     Intent intent = new Intent(context,SecondActivity.class);
     startActivityForResult(intent,GET_LOCATION_LIST); 

step 2: now in Second Activity you have to send the location list back in Main Activity.

//Second Activity

        Intent intent = new Intent();
        intent.putExtra("updated", locationsavailable);
        setResult(Activity.RESULT_OK, intent);
        SubmissionMailerClientContactList.this.finish();

step 3: now again in main activity you have to receive that intent using onActivityResult()

//Main Activity

         @Override
                protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                    super.onActivityResult(requestCode, resultCode, data);

                      if (resultCode == RESULT_OK) {           
    DefaultLocations=(ArrayList<String>)getIntent().getSerializableExtra("updated");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM