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:
@Override
public void onBackPressed()
{
ActionBar actionbar = getActionBar();
if(actionbar.getSelectedTab().getPosition() == 0)
{
if(!fla.onBackPressed())
super.onBackPressed();
}
}
And this in the fragment:
public boolean onBackPressed()
{
if(back_str.equals(MAIN_POINT)){
Log.v("A", "BACK FIRST" + back_str);
return false;
}
else
{
Log.v("A", "BACK PREMUTO");
updateListViewFolder(back_str);
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?
EDIT:
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
songsListData.removeAll(songsListData);
//AGGIUNGO LA CARTELLA PRECEDENTE
File here = new File(newPath);
back_str = here.getParent();
Log.v("A", "S:"+back_str);
Song back = new Song(back_str, true); //Sono directory
songsListData.add(back);
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
songsListData.add(sdir);
}
}
getListView().invalidateViews();
}
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
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
else
ft.add(mFragment, "F0");
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
FolderListActivity f1 = (FolderListActivity) getFragmentManager().findFragmentByTag("F1"); //accedo allo specifico fragment
if(f1 != null)
f1.cleanView();
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// do nothing
}
}
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.