2015-08-17 69 views
1

我有FragOne調用FragTest和FragTest調用FragBook的順序調用的框架。當我按下後退按鈕時,當前的片段是FragBook,FragTest僅顯示爲分秒,然後我的應用程序被髮送到應用程序托盤。片段onKeyDown()不按預期工作

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    FragmentManager fragManager = getSupportFragmentManager(); 
    Fragment frag = fragManager.findFragmentByTag("Frag_Book"); 

    if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
     if (frag.isVisible()) { 
      fragManager.beginTransaction() 
        .replace(R.id.container, Fragment.instantiate(MainActivity.this, fragments[1]),"Frag_Test") 
        .commit(); 
     } 
    } 
    return super.onKeyDown(keyCode, event); 
} 

我在哪裏弄錯了方法?謝謝...

+3

也許你應該在分段事務後返回true? – Karakuri

+0

你在片段中調用onKeyDown? – Rich

+0

不,我把它叫做所有碎片的基本活動......我都很困惑......所以發生這種事是因爲返回語句...... – Jerry

回答

0
@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     FragmentManager fragManager = getSupportFragmentManager(); 
     Fragment frag = fragManager.findFragmentByTag("Frag_Book"); 

     if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
      if (frag.isVisible()) { 
       fragManager.beginTransaction() 
         .replace(R.id.container, Fragment.instantiate(MainActivity.this, fragments[1]),"Frag_Test") 
         .commit(); 
      return true; //"------------- should be add" 
      } 
     } 
     return super.onKeyDown(keyCode, event); 
    }