我在选项卡选项中有一个包含不同片段的活动,我想覆盖后退按钮,以便它调用片段中的方法,将listview更新为父文件夹列表。 现在我已经在主要活动中做到了这一点:
@Override
public void onBackPressed()
{
ActionBar actionbar = getActionBar();
if(actionbar.getSelectedTab().getPosition() == 0)
{
if(!fla.onBackPressed())
super.onBackPressed();
}
}
这在片段中:
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是类中的全局字符串,引用listview中的当前父目录,每次打开文件夹时都会更新。
似乎当我按下后退按钮时back_str总是= null
有人可以帮助我并告诉我一种解决方法吗?
编辑:
我会尝试更好地解释一下,我在片段中有一个listview,用于导航到手机的文件夹,从/ storage /当我按下它打开的文件夹时更新listview调用此方法:
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();
}
现在我想返回父文件夹,如果选择了后退按钮,但是当我尝试从活动中调用片段onBackPressed时,我会收到错误。我正在使用一个静态变量string_str来存储我的片段中我要更新列表的父文件夹的路径,但不知怎的,我无法使用相同的updateListViewFolder方法更新列表
答案 0 :(得分:1)
好的,我发现了错误。
我没有在操作栏选项卡中调用片段实例的方法,我只是调用该类的方法。
我让我的类MyTabListener为我的片段设置了一个标签,以便我可以访问它
(FolderListActivity) getFragmentManager().findFragmentByTag("F1");
这里的课程:
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
}
}