Example usage for android.support.v4.app FragmentActivity onBackPressed

List of usage examples for android.support.v4.app FragmentActivity onBackPressed

Introduction

In this page you can find the example usage for android.support.v4.app FragmentActivity onBackPressed.

Prototype

public void onBackPressed() 

Source Link

Document

Take care of popping the fragment back stack or finishing the activity as appropriate.

Usage

From source file:org.mariotaku.twidere.fragment.MessagesConversationFragment.java

@Override
public boolean handleKeyboardShortcutSingle(@NonNull KeyboardShortcutsHandler handler, int keyCode,
        @NonNull KeyEvent event, int metaState) {
    final String action = handler.getKeyAction(CONTEXT_TAG_NAVIGATION, keyCode, event, metaState);
    if (ACTION_NAVIGATION_BACK.equals(action)) {
        final boolean showingConversation = isShowingConversation();
        final EditText editText = showingConversation ? mEditText : mEditUserQuery;
        final boolean textChanged = showingConversation ? mTextChanged : mQueryTextChanged;
        if (editText.length() == 0 && !textChanged) {
            final FragmentActivity activity = getActivity();
            if (!mNavigateBackPressed) {
                Toast.makeText(activity, R.string.press_again_to_close, Toast.LENGTH_SHORT).show();
                editText.removeCallbacks(mBackTimeoutRunnable);
                editText.postDelayed(mBackTimeoutRunnable, 2000);
                mNavigateBackPressed = true;
            } else {
                activity.onBackPressed();
            }/* w w w  .  j  a va 2  s  . com*/
        } else {
            mQueryTextChanged = false;
            mTextChanged = false;
        }
        return true;
    }
    return false;
}

From source file:org.mariotaku.twidere.fragment.support.MessagesConversationFragment.java

@Override
public boolean handleKeyboardShortcutSingle(@NonNull KeyboardShortcutsHandler handler, int keyCode,
        @NonNull KeyEvent event) {
    final String action = handler.getKeyAction(CONTEXT_TAG_NAVIGATION, keyCode, event);
    if (ACTION_NAVIGATION_BACK.equals(action)) {
        final boolean showingConversation = isShowingConversation();
        final EditText editText = showingConversation ? mEditText : mEditUserQuery;
        final boolean textChanged = showingConversation ? mTextChanged : mQueryTextChanged;
        if (editText.length() == 0 && !textChanged) {
            final FragmentActivity activity = getActivity();
            if (!mNavigateBackPressed) {
                final SuperToast toast = SuperToast.create(activity, getString(R.string.press_again_to_close),
                        Duration.SHORT);
                toast.setOnDismissListener(new OnDismissListener() {
                    @Override//  w w  w.j av  a  2s .  com
                    public void onDismiss(View view) {
                        mNavigateBackPressed = false;
                    }
                });
                toast.show();
                mNavigateBackPressed = true;
            } else {
                activity.onBackPressed();
            }
        } else {
            mQueryTextChanged = false;
            mTextChanged = false;
        }
        return true;
    }
    return false;
}