Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.content.SharedPreferences;

import android.preference.PreferenceManager;

public class Main {
    private static SharedPreferences sPrefs;
    private static Context sCurrentContext;

    /**
     * <pre>
     * Get default {@link SharedPreferences} of the app.
     * </pre>
     */
    public static SharedPreferences getPrefs() {
        if (sPrefs == null) {
            sPrefs = PreferenceManager.getDefaultSharedPreferences(getCurrentContext());
        }
        return sPrefs;
    }

    /**
     * <pre>
     * Get current context of the app. This method resolves the inconvenience of Android which requires context for most of its API.
     * If no activity is resumed, this method returns application context. Otherwise, this method returns last resumed activity.
     * </pre>
     */
    public static Context getCurrentContext() {
        return sCurrentContext;
    }
}