Home > OS >  How to return ArrayList from intent
How to return ArrayList from intent

Time:03-24

I have a code that pass an arraylist to another intent, how can i return the value of updated arraylist back to the main intent ?

Array list in main intent code :

ArrayList<Users> vuser = new ArrayList<>();
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
intent.putParcelableArrayListExtra("vuser", vuser);
startActivity(intent);

do i have to pass the arraylist again to the main intent ? like this :

Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
intent.putParcelableArrayListExtra("vuser", vuser);
startActivity(intent);

CodePudding user response:

You can pass it using the getIntent() method. When you go back, you can override the onBackPressed() method and add your own login like the one given below. Check the code out:

@Override
public void onBackPressed(){
   getIntent().putParcelableArrayListExtra("vuser", vuser);
   finish();
}

This will send data to the previous activity and then finish the current activity.

Hope it helps

  • Related