Anusha
Anusha

Reputation: 969

How getIntent.hasExtra method works in android

I have been following a tutorial from this link which is basically about File browse concept in android. Everything works fine but I am getting confused of how passing intents between activities works in android after reading this link. The First activity is as follows,

public class MainActivity extends Activity implements OnClickListener {

private static final int REQUEST_PICK_FILE = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    filePath = (TextView)findViewById(R.id.file_path);
    Browse = (Button)findViewById(R.id.browse);
    Browse.setOnClickListener(this);      
}

public void onClick(View v) {

    switch(v.getId()) {

    case R.id.browse:
        Intent intent = new Intent(this, FilePicker.class);            
        startActivityForResult(intent, REQUEST_PICK_FILE);
        break;
    }
}

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

    if(resultCode == RESULT_OK) {

        switch(requestCode) {

        case REQUEST_PICK_FILE:

            if(data.hasExtra(FilePicker.EXTRA_FILE_PATH)) {

                selectedFile = new File
                        (data.getStringExtra(FilePicker.EXTRA_FILE_PATH));
                filePath.setText(selectedFile.getPath());  
            }
            break;
        }
    }

As far as I understood, its passing the intent "REQUEST_PICK_FILE" and based on what it returns from "FilePicker.Class" , it will perform the action because its StartActivityOnResult. Confusion starts from next activity. Here is the File Picker class,

  public class FilePicker extends ListActivity {

public final static String EXTRA_FILE_PATH = "file_path";
public final static String EXTRA_SHOW_HIDDEN_FILES = "show_hidden_files";
public final static String EXTRA_ACCEPTED_FILE_EXTENSIONS = "accepted_file_extensions";
private final static String DEFAULT_INITIAL_DIRECTORY = "/";
.......
protected String[] acceptedFileExtensions;

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

    .........
    // Initialize the extensions array to allow any file extensions
    acceptedFileExtensions = new String[] {};

    // Get intent extras
    if(getIntent().hasExtra(EXTRA_FILE_PATH)) 
        Directory = new File(getIntent().getStringExtra(EXTRA_FILE_PATH));

    if(getIntent().hasExtra(EXTRA_SHOW_HIDDEN_FILES)) 
        ShowHiddenFiles = getIntent().getBooleanExtra(EXTRA_SHOW_HIDDEN_FILES, false);

    if(getIntent().hasExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS)) {

        ArrayList<String> collection = 
                getIntent().getStringArrayListExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS);

        acceptedFileExtensions = (String[]) 
                collection.toArray(new String[collection.size()]);
    }
}
 ..............

Whats actually happening here? what does the lines ,

   if(getIntent().hasExtra(EXTRA_SHOW_HIDDEN_FILES)) 
        ShowHiddenFiles = getIntent().getBooleanExtra(EXTRA_SHOW_HIDDEN_FILES, false); 

actually mean? We are not passing any extras like, "EXTRA_SHOW_HIDDEN_FILES" from previous activity and even EXTRA_SHOW_HIDDEN_FILES has been declared in this class. I seriously don't understand what's happening between these two activities . I am not able to proceed before understanding what's actually going on. Any help would be really great !! Thanks.

Upvotes: 1

Views: 3948

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

what does the lines... actually mean?

It means that if the Intent used to start FilePicker has an EXTRA_SHOW_HIDDEN_FILES, hold onto that value in what I assume is a boolean field on the activity that is not shown in your redacted code listing.

We are not passing any extras like, "EXTRA_SHOW_HIDDEN_FILES" from previous activity

You could, though. You do not have to.

even EXTRA_SHOW_HIDDEN_FILES has been declared in this class

That is fairly typical. FilePicker is declaring an API, and so it exposes the names to be used for incoming and outgoing extras. EXTRA_SHOW_HIDDEN_FILES is public, and so it can be referenced from anywhere, including MainActivity.

Upvotes: 3

Related Questions