Pievis Pievis - 11 months ago 46
Android Question

overriding back button in fragment

I have an activity with different fragments in the tab selection, I want to override the back button so that it calls a method in the fragment that update the listview to the parent folder list.
For now I've made this in the main activity:

public void onBackPressed()
ActionBar actionbar = getActionBar();
if(actionbar.getSelectedTab().getPosition() == 0)

And this in the fragment:

public boolean onBackPressed()
Log.v("A", "BACK FIRST" + back_str);
return false;
Log.v("A", "BACK PREMUTO");
return true; //torna indietro nella view

back_str is a global string in the class referring to the current parent directory in the listview and it's updated every time I open a folder.

Seems that when I press the back button back_str is always = null

can someone help me and tell me a way I can solve this?


I'll try to explain me better, I've a listview inside the fragment that I use to navigate into the folder of the phone, starting from /storage/ when I press a folder it opens updating the listview calling this method:

public void updateListViewFolder(String newPath)
ArrayList<Song> songsListTMP = new SongManager().getPlayList(newPath); //aggiungo tutte le canzoni

File here = new File(newPath);
back_str = here.getParent();
Log.v("A", "S:"+back_str);
Song back = new Song(back_str, true); //Sono directory

for(int i = 0; i < songsListTMP.size(); i++)
Song song = songsListTMP.get(i);
songsListData.add(song); //tengo un attimo separate

for(File dir : here.listFiles() )
if(dir.isDirectory() && dir.canRead()){
Song sdir = new Song(dir.getPath(), true); //Sono directory



Now I want to return to the parent folder if the back button is selected, but somehow I get errors when I try to call the method onBackPressed of the fragment from the activity. I'm using a static variable string_str to store inside my fragment the path of the parent folder I want to update the list with, but somehow I can't get the list updated with the same updateListViewFolder method

Answer Source

Ok I've found the error. I wasn't calling the method of the instance of the fragment in the actionbar tab, I was just calling the method of that class. I made my class MyTabListener to set a tag to my fragment so that I could access to it with (FolderListActivity) getFragmentManager().findFragmentByTag("F1");

Here the class:

class MyTabListener implements ActionBar.TabListener {
    private Fragment mFragment;

    public MyTabListener(Fragment fragment) {
        mFragment = fragment;

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if(mFragment instanceof FolderListActivity)
            ft.add(mFragment, "F1"); //mi occupo di settare un tag per recuperare la specifica istanza del fragment
            ft.add(mFragment, "F0");

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        FolderListActivity f1 = (FolderListActivity) getFragmentManager().findFragmentByTag("F1"); //accedo allo specifico fragment
        if(f1 != null)

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // do nothing