1

i am trying to handle the onBackPressed diferently for every fragment. for this i want to use a switch statement with cases dependedn on the fragment that is currently showing. in my Main i have. but dont know what to use in the switchstatement.

public class MyActivity extends Activity
{
    @Override
    public void onBackPressed(){
        super.onBackPressed();
switch(........){
        case (1):
        Fragment1.onBackPressed();
        case (2):
        Fragment2.onBackPressed();
    }
}

in my fragments:

public class Fragment1 extends Fragment
{
    //My created method
    public static void onBackPressed()
    {
        // make it do what you want.
    }
}

so my question, how can i use a switchstament where de cases are depended on what fragment is showing at this moment.

D.Blazer
  • 192
  • 1
  • 2
  • 12

2 Answers2

0

In on onBackPressed(). You can check current visible Fragment

public class MyActivity extends Activity
{
    @Override
    public void onBackPressed(){
        if(yourFragment1.isVisible()) {
           ...
        }else if(yourFragment2.isVisible(){
           ...
        }
    }
}
Linh
  • 57,942
  • 23
  • 262
  • 279
0

Don't use such switch in single onBackpressed of activity but you can get the key event of each fragment object and do like this,

fragment.getView().setFocusableInTouchMode(true); //Don't miss this line
fragment.getView().setOnKeyListener( new OnKeyListener()
{
    @Override
    public boolean onKey( View v, int keyCode, KeyEvent event )
    {
        if( keyCode == KeyEvent.KEYCODE_BACK )
        {
           //Do what you want
            return true;
        }
        return false;
    }
} );
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • hello, from what i have found/heard so far is that this wil not work when using for example edittext, of wich i have multiple. cause that will get focus – D.Blazer Mar 11 '16 at 10:00
  • hey, i found a way to make this work, hoever this requires me to handle the event for every edit textfield seperatly, wich doesnt really seem like the best method. – D.Blazer Mar 14 '16 at 09:10
  • no but when u are inside an edittext it gets focus and will not do the keyevent of the fragment – D.Blazer Mar 14 '16 at 09:14
  • But the back key and clear key of soft keyboard are different.. Are you sure keyevent won't work when we are focused inside edittext ? – Shree Krishna Mar 14 '16 at 09:18
  • regretably i am, atm i am handeling the seperate with `editName.setOnKeyListener(new View.OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK){ backButton.toClientsFragment(); } return false; } });` this does work, but probably isnt the best way – D.Blazer Mar 14 '16 at 09:21
  • Then you can implement OnKeyListener in Fragment or Activity – Shree Krishna Mar 14 '16 at 09:29
  • hmm, that wont work either, inside fragment for the reason we just discussed. in activity its not posible cause i have mupltiple fragments handled by same activity, and all fragment need to handle onbackpressed in a seperate way. i was thinking of in the activity aswel with switch, but cant find a way to it like that – D.Blazer Mar 14 '16 at 09:41
  • Have you tried addOnBackStackChangedListener as showin [here](http://stackoverflow.com/questions/18190047/using-onbackpressed-in-android-fragments) – Shree Krishna Mar 14 '16 at 10:01