2013-09-29 79 views
1

我有一个FragmentActivity。 活动包含一个片段。
在片段中,有一个按钮,当点击一个imageview时会出现(View.VISIBLE)在中心。
我想要一个后退按钮事件来检查如果imageview是可见的,然后隐藏它,否则,继续默认的后退按钮事件。
由于FragmentActivity和Fragment是单独的类。片段中没有onBackPressed()。那我该怎么做呢?我想处理Fragment类中的back事件。Android:FragmentActivity中的后退按钮事件

回答

0
@Override 
public void onBackPressed() { 

    // If the fragment is here, let him handle it 
    ourFragment.someMethodWeveCreatedToHandleBackPressed(); 

    // If it was not handled by the method above, then let the super do his usual "back" 
    if (!handled){ 
     super.onBackPressed(); 
    } 
} 
+0

该活动可能包含不同的片段。那么如何一个接一个地调用上面的方法呢? – jjLin

+0

http://stackoverflow.com/questions/6102007/is-there-a-way-to-get-references-for-all-currently-active-fragments-in-an-activi – Sean

-2

使用transaction.addToBackStack(空);

+0

它适用于我。 – Swetank

+1

它不会工作,因为'ImageView'在同一个片段内。只有一个片段。 – yugidroid

0

你为什么不重写onKeyDown方法在你的片段?

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     // Check you image view visibility 
     if(yourImageView.getVisibility() == View.VISIBLE) { 
       yourImageView.setVisiblity(View.GONE); 
       return true; // This line is important to handle the event here and not in the next receiver 
     } 
    } 
    return super.onKeyDown(keyCode, event); 
} 

让我知道它是否适用于您!