0

I develop an app for video surveillance. I use preferences to keep cameras settings. In preferences I create main screen with list of cameras, when user click on current item, it opens subscreen with camera name, ip, port etc. When user entered camera settings and clicked back button to return to main screen, I need to check that user entered all necessary data - name, ip, port, usr, pwd and if one of parameters missed, the app will show toast with warning.

The question is, how can I determine that user clicked on back button?

In PreferenceFragment, onBackPressed() and onKeyDown() not work, moreover I think it's not correct use this methods in my case...

http://4pda.ru/forum/dl/post/5659206/Screen-Main.gif - Main screen

http://4pda.ru/forum/dl/post/5659204/Screen-Sub.gif - Subscreen

My code:

public class MyFragments extends PreferenceFragment {
....
@Override
public void onCreate(Bundle savedInstanceState) {
root = getPreferenceManager().createPreferenceScreen(getActivity());
setPreferenceScreen(createMyPrefenceScreen());
}

private PreferenceScreen createMyPrefenceScreen() {
for (int i=1; i<=8; i++) {
PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(getActivity());
screen.setKey("screen_name_" + i);
screen.setTitle("Number " + i);

ip = new EditTextPreference(getActivity());
ip.setKey("ip_" + i);
ip.setTitle("IP ");
screen.addPreference(ip);
ip.setSummary(ip.getText());
....
root.addPreference(screen);
}
return root;
}
...
} 
apollox
  • 101
  • 7
  • Why dont check as the user is entering? – Biu Jan 15 '15 at 10:17
  • Because I need check after user have entered name, ip, port, user, pwd - full set of data. When user press back button, it means that he complete camera's configuration and going back to main screen. And if some parameter missed, app shows the message. – apollox Jan 15 '15 at 10:58

1 Answers1

0

You have to implement onBackPressed in your Activity, not in the PreferenceFragment.

Following 2nd answer given to this question:

private final static String TAG_FRAGMENT = "TAG_FRAGMENT";

private void showFragment() {
    final Myfragment fragment = new MyFragment();
    final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
    transaction.addToBackStack(null);
    transaction.commit();
}

@Override
public void onBackPressed() {
    final Myfragment fragment = (Myfragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);

    if (fragment.insertedDataIsCorrect()) { // HERE you can check for correct data and allow back pressed or not
        super.onBackPressed();
    }
}
Community
  • 1
  • 1
Alberto Malagoli
  • 1,173
  • 10
  • 14
  • Thanks! I have implemented your code. The problem is, it works when I press back from main screen to previous activity. But, this doesn't work when I press back button from subscreen to main screen... – apollox Jan 15 '15 at 10:49
  • I don't understand what you mean... Anyway, you probably have to adapt the code to your situation. – Alberto Malagoli Jan 15 '15 at 10:55
  • Ok ) Preference Subscreen ->(#1 back button pressed)-> Preference Main Screen ->(#2 back button pressed)-> Previous Activity. #1 - not working, #2 - ok. I don't now why ( – apollox Jan 15 '15 at 11:05
  • Take a look at [this article](http://vinsol.com/blog/2014/10/01/handling-back-button-press-inside-fragments/) – Alberto Malagoli Jan 15 '15 at 11:38