[英]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. 我在选项卡选择中有一个包含不同片段的活动,我想覆盖后退按钮,以便它调用片段中的方法,该方法将listview更新为父文件夹列表。 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. back_str是类中的全局字符串,它引用列表视图中的当前父目录,并且每次打开文件夹时都会更新。
Seems that when I press the back button back_str is always = null 似乎当我按下后退按钮back_str总是= 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: 我会尽力向我解释,我在片段中使用了一个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();
}
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. 现在,如果选择了后退按钮,我想返回到父文件夹,但是当我尝试从活动中调用片段的onBackPressed方法时,却以某种方式出现错误。 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
我正在使用静态变量string_str在片段中存储要用于更新列表的父文件夹的路径,但是以某种方式我无法使用相同的updateListViewFolder方法更新列表
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");
我将类MyTabListener设置为片段的标签,以便可以使用
(FolderListActivity) getFragmentManager().findFragmentByTag("F1");
访问该片段(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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.