org.badreader.client.fragments.BaseFragment.java Source code

Java tutorial

Introduction

Here is the source code for org.badreader.client.fragments.BaseFragment.java

Source

/* 
 * Author: Eugene Sakara (eresid@gmail.com)
 * URL: https://github.com/eresid/badreader
 * License: Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html)
 * Start: 24.05.2014
 */

package org.badreader.client.fragments;

import org.badreader.client.MainActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class BaseFragment extends Fragment {

    public MainActivity mActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = (MainActivity) this.getActivity();
    }

    protected MainActivity getMainActivity() {

        if (mActivity == null) {
            System.exit(0);
        }

        return mActivity;
    }

    public boolean onBackPressed() {
        return false;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    protected void hideKeyboard(Context _context, EditText _edit) {
        InputMethodManager imm = (InputMethodManager) _context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(_edit.getWindowToken(), 0);
    }

    protected void hideKeyboard(Activity _activity) {
        try {
            InputMethodManager imm = (InputMethodManager) _activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(_activity.getCurrentFocus().getWindowToken(), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void showKeyboard(final Activity _context, final EditText _edit) {
        final InputMethodManager imm = (InputMethodManager) _context.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (android.os.Build.VERSION.SDK_INT < 11) {
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }

        _edit.postDelayed(new Runnable() {
            public void run() {
                _edit.requestFocus();
                imm.showSoftInput(_edit, 0);
            }
        }, 100);
    }
}