0

I have a fragment where I toggle visibility between View1 and View 2. The flow I would like is after entering View 2 from View1 I want to be able to make View 1 visible using the back button.

Is it possible to override the back button when View2 is visible to change the visibility of View1? If not, is the only way to split this fragment into 2 fragments and use the back stack with fragment manger?

Matt
  • 1,017
  • 2
  • 11
  • 27

2 Answers2

0

I would split it into two fragments, but it is not the only way.

You can override the onKey( View v, int keyCode, KeyEvent event ) method on a view, but it will only work if the view is in focus.

See more at: Android Fragment handle back button press

Community
  • 1
  • 1
0

You can override back button behavior:

@Override
public void onBackPressed() {
   //your code 
}

If you want to check if fragment is visible try to use this method:

yourFragmentInstance.getUserVisibleHint()

More information here: https://developer.android.com/reference/android/app/Fragment.html#getUserVisibleHint() https://developer.android.com/reference/android/app/Activity.html#onBackPressed()

Hope this helps!

brut
  • 327
  • 1
  • 11