0

I am making a music player application, and I am trying to implement playlists. I have a file chooser in another intent, and I would like the ListView in the mainActivity to update when the file chooser intent closes. how can I call my UpdateListView method when it closes?

start intent:

Intent intent = new Intent(this, FileChooser.class);
startActivity(intent);

Closing intent

public void closeButton(View view){
    finish();
}

Any help would be appreciated! thanks!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Alex Collette
  • 1,664
  • 13
  • 26
  • You want to use `startActivityForResult` – OneCricketeer Dec 04 '16 at 02:14
  • Refer. http://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android – OneCricketeer Dec 04 '16 at 02:15
  • one option is override onResume() function in your Activity. When your chooser activity close and your Listview comes in foreground.Check if data is updated may be some static data, if yes update ListView. Best option is to implement onActivityResult() and call FileChooser with startActivtyForresult() – Swapnil Dec 04 '16 at 02:17

2 Answers2

0

I assume you are using your own FileChoser class, not a standard Android one:

private static final int FileChooserRequestCode = 666;

Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FileChooserRequestCode);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == FillChooserRequestCode) {
     if (resultCode == Activity.RESULT_OK) {
          //  ... file is chosen
          String fileName = data.getStringExtra("FileName");    
     } else {

           ... dialog is closed

     }

   }
}

in FileChoser you do

 Intent intent = new Intent();
 intent.putStringExtra("FileName", fileName);
 SetResult(Activity.RESULT_OK, intent);
 finish();

and

 SetResult(Activity.RESULT_CANCELED);
 finish();
cyanide
  • 3,885
  • 3
  • 25
  • 33
0

You can use startActivityForResult() please refer the link Getting Results From Activity

static final int FILE_CHOOSER_INTENT = 1;  // The request code
...
private void chooseFile() {
    Intent intent = new Intent(this, FileChooser.class);
    startActivityForResult(intent, FILE_CHOOSER_INTENT);
}

Call setResult pass your result data as Intent. for details refer link SetResult function

Override this in your calling activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == FILE_CHOOSER_INTENT) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}
Swapnil
  • 2,409
  • 4
  • 26
  • 48